Tim Hunter
10/8/2006 11:26:00 PM
Tuka Opaleye wrote:
> Hi,
>
> I am trying to get my head around the || symbol used extensively in
> ruby. To me it is somewhat foreign still and I am trying a few exercise
> to make it click. Can anyone suggest a phrase (pseudolanguage) that
> accurately expresses the possible expressions using || ?
>
> For example, see the comments and please correct me if I am wrong. Could
> you add other cases that I may not have caught here ?
>
> input.each_byte do |b|
> case b
> when ?\C-c; puts 'Control-C: stopped a process?'
> when ?\C-z; puts 'Control-Z: suspended a process?'
> when ?\n; puts 'Newline.'
> when ?\M-x; puts 'Meta-x: using Emacs?'
> end
> for each byte in input, assign it to b...
>
>
>
> Ex 2:
> open('smiley.html', 'wb') do |f|
> f << '<meta http-equiv="Content-Type"
> content="text/html;charset=UTF-8">'
> f << "\xe2\x98\xBA"
> end
>
> Sugestions ?.....
>
>
> Ex 3:
> octal = "\000\001\010\020"
> octal.each_byte { |x| puts x }
> for each byte in octal, assign an x...
>
> TIA,
> Tuka
>
>
In these examples the || delimit the argument list to the block, similar
to the way parentheses delimit the argument list in a method definition.
When the method yields to the block it passes some value or values from
the method to the block:
yield(arg1, arg2, arg3)
and these values become the block arguments:
obj.meth { |a, b, c| ....}
In the above case arg1 is the 'a' argument, 'arg2' is the 'b' argument,
and 'arg3' is the 'c' argument. A method may yield multiple times,
passing a new set of arguments each time, as each_byte does in examples
1 and 3, or it may yield only once, as in example 2.