From: Rick DeNatale Date: 2007-10-22T10:30:34+09:00 Subject: Re: gsub does not work with do/end block, only brackets On 10/21/07, Michael Chow wrote: > puts "abcdefg".gsub(/abc/) do |x| > x.upcase > end > > ^This does not work. ArgumentError, it's expecting the second argument > that's required when a block is not provided. > > puts "abcdefg".gsub(/abc/) { |x| > x.upcase > } > > ^This works. > > Why?? Because {} binds tighter than do end puts "abcdefg".gsub(/abc/) do |x| x.upcase end Is parsed as: > puts "abcdefg".gsub(/abc/) { |x| > x.upcase > } > > ^This works. > > Why?? Because {} binds tighter than do end (puts ("abcdefg".gsub(/abc/))) do |x| x.upcase end Which means that the block is associated with the call to puts rather than gsub. While puts "abcdefg".gsub(/abc/) { |x| x.upcase \ } Is parsed as puts ("abcdefg".gsub(/abc/) { |x| x.upcase \ }) -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/