Ruby puzzler: gsub, blocks, and procs

See if you can guess what this code will do before you run it in ruby.

  upc = Proc.new {|m| $1.upcase}

  puts "hello world".gsub(/([aeiou])/, &upc)
  puts "hello world".gsub(/(\w)/, &upc)

  def doit(str, re, blk)
    puts str.gsub(re, &blk)
  end

  doit "hello world", /([aeiou])/, upc
  doit "hello world", /(\w)/, upc

Now try running it in JRuby. Whoa.

Get In Touch