Learning Ruby

Simple Substitution with Block

If a block is provided to sub or gsub then the matched string is passed onto the block and the return value of the block is used to do the replacement.


"Superman".gsub(/[aeiou]/) {|m| m.upcase } 
# => "SUpErmAn"

 

 

January 08, 2010 at 12:11 pm