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
imagecontains four elements, each of which is an array of four elements. Had it been what you might think of as a "three-dimensional" array, it might contain four elements that are arrays, each of which is an array of four elements that are themselves arrays of, say, three elements.[1, 3.1, "cat", :dog, 4..7, [5, {3=>{:a=>[4,5]}}]].