Wednesday, August 20, 2008

Programming Ruby 3rd - new topics

  • expressions: method chain: def <<(other); total += other; self end
  • expressions: define []=: def []=(*params);  obj.[]=(1,2) # => [1,2]
  • expressions: backticks also a method of Kernel, that can redefine.
  • parallel assignment: f, *, l = 1,2,3,4,5 # f = 1, l = 5
  • nested assignment: a, (b,c), d = 1,2,3,4 # a = 1, b = 2, c = nil, d = 3
  • flip-flop function with range: while puts line if line=~/third/..line=~/fifth/
  • flip-flop while if (($.==1)||line=~/eig/)..(($.==3)||line=~/nin/); print line
  • redo if line.gsub!(/`(.*?)`/){eval($1)}
  • ary.each do |val; outer-local|
  • block local scope: [1,2,3].each {|a,b|} or {|a;b|}#b is local val of block 

No comments: