From: Alex Gutteridge Date: 2007-10-22T10:36:23+09:00 Subject: Re: gsub does not work with do/end block, only brackets On 22 Oct 2007, at 09:45, 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?? It interprets the block you are trying to give to 'gsub' as a block argument to 'puts' instead. Brackets remove the ambiguity: irb(main):008:0> "abc".gsub(/a/){|x| x.upcase} => "Abc" irb(main):009:0> "abc".gsub(/a/) do |x| x.upcase end => "Abc" irb(main):010:0> puts "abc".gsub(/a/){|x| x.upcase} Abc => nil irb(main):011:0> puts "abc".gsub(/a/) do |x| x.upcase end ArgumentError: wrong number of arguments (1 for 2) from (irb):11:in `gsub' from (irb):11 irb(main):012:0> puts("abc".gsub(/a/) do |x| x.upcase end) Abc => nil Alex Gutteridge Bioinformatics Center Kyoto University