This link has been bookmarked by 15 people . It was first bookmarked on 24 Sep 2007, by swan lin.
-
11 May 12
-
16 Apr 12
-
19 Feb 11
-
In Ruby, anything on a line after a hash mark is a comment and is ignored by the interpreter.
-
eachis a method that accepts a block of code then runs that block of code for every element in a list -
block is like an anonymous function or
lambda. The variable between pipe characters is the parameter for this block -
What happens here is that for every entry in a list,
nameis bound to that list element, and then the expressionputs "Hello #{name}!"is run with that name. -
all the housekeeping details are hidden within the
eachmethod, all you need to do is to tell it what to do with each element. Internally, theeachmethod will essentially callyield "Albert", thenyield "Brenda"and thenyield "Charles", and so on -
real power of blocks is when dealing with things that are more complicated than lists.
-
you can also handle setup, teardown, and errors—all hidden away from the cares of the user
-
say_byemethod doesn’t useeach, instead it checks to see if@namesresponds to thejoinmethod, and if so, uses it.
-
-
07 Jul 10
-
notice the initial lines, which begin with a hash mark (#)
-
Ruby, anything on a line after a hash mark is a comment and is ignored by the interpreter. Th
-
The variable between pipe characters is the parameter for this block.
-
-
13 Nov 09
-
24 Sep 09
-
@names.each do |name|
puts "Hello #{name}!"
end -
eachis a method that accepts a block of code then runs that block of code for every element in a list, and the bit betweendoandendis just such a block. A block is like an anonymous function orlambda. The variable between pipe characters is the parameter for this block.
-
-
28 Jul 09
-
24 Sep 07
-
In Ruby, anything on a line after a hash mark is a comment and is ignored by the interpreter
-
__FILE__is the magic variable that contains the name of the current file. -
$0is the name of the file used to start the program.
-
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.