I'm newbie in Ruby (frankly, I've just started learning it for fun, without any future plans), and I've noticed strange behaviour of loops. I assume my problem comes from lacks in knowledge in the mechanics of Ruby.
boysNames = ["Martin", "Lucas", "John"]
#class only for one method. I know it's not absolutely correct, but huh I'm just learning Ruby
class Human
def initialize(name)
@name = name;
end
String name = ""
def getName()
puts @name;
end
end
Array boys = []
#create objects
for x in boysNames
tempBoy = Human.new(x)
boys.push(tempBoy)
end
#output for class
puts "Method 1: for in loop \n"
for x in boys
puts x.getName()
end
puts "\nMethod 2: manual array[var] \n"
boys[0].getName()
boys[1].getName()
boys[2].getName()
puts "\nMethod 3: .each do \n"
boys.each do |i|
puts i.getName()
end
#output for Array
puts "Method 1: for in loop \n"
for x in boysNames
puts x
end
puts "\nMethod 2: manual array[var] \n"
puts boysNames[0]
puts boysNames[1]
puts boysNames[2]
puts "\nMethod 3: .each do \n"
boysNames.each do |i|
puts i
end
#loop through the boys array
puts "\nboys array: \n"
for x in boys
puts x
end
So my question is: Why when I loop through the array everything is fine, and when I loop through the class, my results are divided by newlines? Are there any "invisible" objects that I couldn't find? As you can see my last loop found only three objects with different places in the memory.
I will be pleased, if the answer contains explanation of "how it works".
Thank you in advance ;)
P.S. Output:
Method 1: for in loop
Martin
Lucas
John
Method 2: manual array[var]
Martin
Lucas
John
Method 3: .each do
Martin
Lucas
John
Method 1: for in loop
Martin
Lucas
John
Method 2: manual array[var]
Martin
Lucas
John
Method 3: .each do
Martin
Lucas
John
boys array:
#<Human:0x007f2a586db788>
#<Human:0x007f2a586db648>
#<Human:0x007f2a586db5d0>
P.S.S. I'm using this as the Ruby interpreter/compiler (I heard that Ruby can also be compiled, so...)