我是红宝石的新手,当我在eclipse (Screenshot)中运行时,一切都很好,我得到了所需的输出但是当我在交互式红宝石中运行它时,我在屏幕截图中得到错误
# MyVector Class
class MyVector
include Enumerable
def initialize (a)
if !(a.instance_of? Array)
raise "ARGUMENT OF INITIALIZER MUST BE AN ARRAY"
else
@array = a
end
end
def toArray
@temp = Array.new(@array.length())
i=0
while(i<@array.length())
@temp[i] = @array[i]
i+=1
end
@temp
end
def to_s
@array.to_s
end
def length
@array.length()
end
def [](i)
@array[i]
end
def each2(a)
raise Error, "INTEGER IS NOT LIKE VECTOR" if a.kind_of?(Integer)
Vector.Raise Error if length != a.length
return to_enum(:each2, a) unless block_given?
length.times do |i|
yield @array[i], a[i]
end
self
end
def * (a)
if a.kind_of? MyVector
Vector.Raise Error if length != a.length
p = 0
each2(a) {|a1, a2|p += a1 * a2}
p
elsif a.kind_of? MyMatrix
@temp = Array.new(a.transpose().length())
i=0
while(i<@temp.length())
@temp[i] = (a.transpose()[i] * self)
i+=1
end
MyVector.new(@temp)
end
end
end
# MyMatrix Class
class MyMatrix
include Enumerable
def initialize a
@array=Array.new(a.length())
i=0
while(i<a.length)
@array[i]=MyVector.new(a[i])
i+=1
end
end
def to_s
"["+@array.map { |i| "'" + i.to_s + "'" }.join(",")+"]"
end
def length
@array.length()
end
def [](i)
@array[i]
end
def transpose
@temp = Array.new(@array.length())
i=0
while(i<@array.length())
@temp[i] = @array[i].toArray()
i+=1;
end
MyMatrix.new(@temp.transpose())
end
def *m
if m.kind_of? MyMatrix
@temp=Array.new(@array.length())
i=0
while (i<@array.length())
@temp[i] = (@array[i] * m).toArray()
i+=1
end
MyMatrix.new(@temp)
end
end
end
答案 0 :(得分:1)
您正在运行的命令(cd D:\
和ruby hwk7.rb
)应该从终端运行,而不是从irb内部运行。退出IRB并尝试再次运行它们。