[Courses] [Ruby] Lesson 2: Containers, Blocks, and Iterators
Anne G
anne at wjh.harvard.edu
Wed Dec 21 19:00:06 EST 2005
> def add(obj)
> @first = [@first,obj]
> end
I got it. In matlab, this would append the obj to an array.
Here @first is a new array, with the old @first as an
element, and obj as a second element. Since this is not an
array, the length method does not work.
--------------------------------------------
It seems that yield/block can help emphasize the goal of the
transaction, and hide the mechanics to do it: When you see
a method with a puts block, you know this line is printing
some info
I also found it useful as a short cut when there is a
process that is the same to support different actions. For
example I rewrote my data reading program with blocks
getinfo {|line| @info1Arr.push("'" +line[6] + "'")}
increaseind
getinfo {|line| @info2Arr.push(line[6])}
increaseind
getinfo {|line| @dataArr.push(line[0]+","+line[2]+","+line[4])}
In this way the same method getinfo was used to extract
different columns in the data file, depending on the block
formula.
anne
More information about the Courses
mailing list