Try it Out! - Iterators & Blocks


So, what am I doing wrong here?

array = [1, 2, 3, 4, 5]

array.each do |x|

x *= 2

print "#{x}"


and tried this

array = [1, 2, 3, 4, 5]

array.each {


x *= 2

print "#{x}"


Used the array named 'odds' too, and used 'print x' instead of '"#{x}"' but did not work. Cleared my cache, with cc and cmd too.
Any tricks here?


I see where you were trying to go with this,

With the array being odds do the following:

odds = [1,3,4,7,9]

odds.each do |x|
    odds = x * 2
    print odds

Right now you're trying to multiply each number in the array, and print that number. While I understand what you were trying to do, that's not the way it needs to be done. At least that's not the way I did it.

Who knows, perhaps your way would work, but you just didn't change the array from 1,2,3,4,5 to 1,3,5,7,9. I'm unsure at this point but I'll look further into it.


Nope your way will work to.

All you need to do is make sure your array has the correct numbers in it and it should work!


Oh, dang, I actually thought about that, but thought "nah, its too obvious" : P
Thanks mate.


This works but destroys the array when the each method ends, since odds is used to store each integer.


Correct. I am not sure what I was thinking when I originally programmed this code, however I saw that it worked and never questioned why. At least now I know :smiley:


I'm actually quite surprised that it works, in a more strict language it would be impossible to do such a thing.

I assume that Ruby makes a clear distinction between an object and its name, which means that there can't be any conflict between the method each using only the object and another instruction using the name to assign it another object. not really sure if that's the correct explanation though,


I think it's because the .each is prevalent inside of the .each loop. It's just an assumption but I believe it's taking the same point in the array and assigning x's value back to that position in the odds array. I could be wrong, but that's what I think is happening


Not sure if I understand what you mean. Are you saying that you think it's assigning each x values to each element of the array?

Because that's not the case, I tried displaying the content of odds after this, and it only contains an integer (the last one), not an array of integers.


Hm.. I'm not sure why it would pass the course with that in that case.. I'm glad you explained that because I wouldn't have known otherwise.


I think it's because their check on this exercise is only on the return of each (which knows the object behind odds since when each is called odds contains the array).

The each method always returns the array on which it worked, so even if odds has changed the content it points to, the object (the array of integers) is still correctly returned by each while the name (odds) now points to something completely different in the same process.

Hence my theory about Ruby making a clear distinction between the use of a name (like odds) and an object, which means that modifying what the name points to while working on the previous object is not a problem for Ruby. But again, that's just a hypothesis. I would love if someone with more knowledge could explain us what's really going on.


I will fix lolman's code:

odds = [1,3,5,7,9]

odds.each do |x|
    odds = x * 2
    print odds

That will work!


this doesn't work for me!:expressionless:


thanks for the help and i had the same problem as this guy some you help us both


here is the code that worked for me

odds = [1,3,5,7,9]

Add your code below!

odds.each do |placeholder|
placeholder *= 2
print "#{placeholder}"

why when I used X in first try it didn't work?
but after used PLACEHOLDER,then try to use X, it works.
Even after reset the exercise. the X works.


odds = [1,3,5,7,9]

Add your code below!

odds.each do
|item| *=2
print odds
this is what i came up with i know it doesnt work but would like to know why