Question

I'm new in ruby, everything is fine when I run it in eclipse (Screenshot) and I get the desired output but when I run it in interactive ruby I get the error in the screen shot

enter image description here

# 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
Was it helpful?

Solution

Those commands you're running (cd D:\, and ruby hwk7.rb) are supposed to be run from the terminal, not from inside irb. Quit IRB and try running them again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top