From: Paul Smith Date: 2009-09-25T17:53:19+09:00 Subject: Re: Roulette & rand On Fri, Sep 25, 2009 at 8:21 AM, Semih Ozkoseoglu wrote: > Hi again Stefano, > > I was looking at the code and as I'm quite new to both programming and > Ruby, I can't seem to understand two things. First is the |i| in > >> [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31,33,35].each >> do |i| > The |i| is a block parameter. The each method essentially calls the code in the following block once for each element in the array, so each in each iteration of the block i will be equal to the current element of the array. Try this in IRB [1,2,3].each {|x| puts x} and you'll see what I mean. > and second is the ':' in why not just 'black' for example. > >>   colors[i] = :black > > If you can find the time to tell me what do these do to make the code > work it would be much apriciated. :black is a Symbol, not a String. I'm not sure exactly how much difference it makes, but from what I've heard it's a good habit to get into. The trick is that the symbol :black is always the exact same object, no matter where you see it in your program. On the other hand, you can have 2 strings with the same contents which will be different objects. Try this in IRB a = "black" b = "black" c = :black d = :black puts a.object_id puts b.object_id puts c.object_id puts d.object_id You'll notice that a and b are different objects, but c and d are the same object. > > Thanks again > Semih > -- > Posted via http://www.ruby-forum.com/. > > -- Paul Smith http://www.nomadicfun.co.uk paul@pollyandpaul.co.uk