2008年4月30日 星期三

to_proc ( &: )

turn the symbol into proc

test= rabbit.map{ |name|  name.upcase }

is the same as 

test= rabbit.map{ &:upcase }


example on rails:

rabbits= Rabbit.find(:all)

//   show the name of rabbits

method 1
 rabbits.collect { |t|  t.name }

method 2
rabbits.collect( &:name)


//  show the name of rabbits using downcase

rabbits.collect(&:name).collect(&:downcase)


沒有留言: