Reputation: 31
I'm still new in Ruby on Rails. Today I'm trying to write some codes which can run the the following:
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
image.output_image
And I'm having trouble setup the initialize. My codes is as below, can someone help me? Thanks a lot
class Subary
attr_accessor :num1, :num2, :num3, :num4
def initialize (num1, num2, num3, num4)
self.num1 = num1
self.num2 = num2
self.num3 = num3
self.num4 = num4
end
def output_subary
puts "#{num1}#{num2}#{num3}#{num4}"
end
end
# subary = Subary.new(0,0,0,0)
# puts subary.output_subary
class Image
def initialize
@subarys = []
@subarys << Subary.new(:num1, :num2, :num3, :num4)
@subarys << Subary.new(:num1, :num2, :num3, :num4)
@subarys << Subary.new(:num1, :num2, :num3, :num4)
@subarys << Subary.new(:num1, :num2, :num3, :num4)
end
def output_image
@subarys.each do |list|
list.output_subary
end
end
end
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
image.output_image
Upvotes: 2
Views: 196
Reputation: 34338
`initialize': wrong number of arguments (1 for 0)
This error means that, initialize
method does not take any argument (0), but you passed one argument to it. Change the initialize
method's definition in your Image
class. Then, it should work.
class Subary
attr_accessor :num1, :num2, :num3, :num4
def initialize(sub_array)
self.num1 = sub_array[0]
self.num2 = sub_array[1]
self.num3 = sub_array[2]
self.num4 = sub_array[3]
end
def output_subary
puts "#{num1}#{num2}#{num3}#{num4}"
end
end
# subary = Subary.new(0,0,0,0)
# puts subary.output_subary
class Image
def initialize(array_of_arrays)
@subarys = []
@subarys << Subary.new(array_of_arrays[0])
@subarys << Subary.new(array_of_arrays[1])
@subarys << Subary.new(array_of_arrays[2])
@subarys << Subary.new(array_of_arrays[3])
end
def output_image
@subarys.each do |list|
list.output_subary
end
end
end
image = Image.new([
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 0, 1],
[0, 0, 0, 0]
])
image.output_image
# => 0000
# => 0100
# => 0001
# => 0000
Upvotes: 1