From: Stefano Crocco Date: 2009-09-25T18:10:02+09:00 Subject: Re: Roulette & rand On Friday 25 September 2009, 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| I don't know whether you've already read about blocks in ruby. Given your question, I don't think so but, in case you did, you can skip the first part of my answer. One of the most peculiar characteristics of ruby are blocks. As the name says, they are blocks of code which can be passed to methods and will be called by those methods at a later time. They can be introduced either with the syntax do ... end or with {...} The ... are actually the body of the block, that is the code which will be executed later. For example, the code 5.times do puts "Hello" end passes a block whose body is puts "Hello" to the "times" method of the object 5. The documentation for the times method says that n.times calls the block n times, that is it executes the contents of the block n times. In the example above, n is 5, so times will call execute the body of the block 5 times. The output is then: Hello Hello Hello Hello Hello Now, I can answer your question: what does [2,4,6].each do |i| ... end mean? As you can see, there are a do and an end keyword, so this must be a block. The difference with the example above is the |i|. At the very beginning of the block, immediately after the do keyword or the opening brace, a number of comma-separated variables (just one, in this case) enclosed in a pair of pipe characters (|) are the arguments passed to the block. Passing arguments to a block works in a similar way as passing arguments to a method. In the code above, for example, the each method of class Array is called. According to the documentation, the each method calls the block for every element of the array, passing the element itself to the block. This means that, inside the body of the block, the variable i will contain one of the elements of the array in turn. For example: [2,4,6].each do |i| puts i end outputs: 2 4 6 "each" calls the block three times: the first the argument is 2, so the variable i is set to 2; the second time i is set to 4 and the third time i is set to 6. This is only a very short introduction to blocks. For more, you can look at http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_containers.html (the section called "Block and Iterators") > |and second is the ':' in why not just 'black' for example. In ruby, a sequence of letters, number, underscores (and some other characters) preceeded by a column (for example, :black) is a Symbol. Symbols are similar to strings. The main differences is that while every time I write a string literal, for example "hello", I create a different string, with symbols this doesn't happen: if I write the symbol :hello one hundred times, I still get the same object every time. If I had written "hello" one hundred times, I'd have got one hundred strings all containing the word "hello". Expecially when beginning programming in ruby, it may be difficult to understand when it's better to use a symbol rather than a string (you can find a lot of material on this topic searching google for something like "strings and symbols"). Symbols are often used as hash keys when you know in advance which keys are allowed. For example, in your program you already know that the only possible outcomes of a roll are black, red or zero, so you can use a symbols as keys. A situation in which symbols wouldn't be a good choice is if you're storing information about people in the hash, using each person's name as keys. Obviously, in this case you don't know in advance which would be the keys of the array, so you should use strings instead. If you know C or C++, symbols are somewhat similar to enums, except that they don't have a numerical value associated with them. > | > |> 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. I hope this helps > |Thanks again > |Semih Stefano