Ad
  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Not sure if you're aware, but I noticed a couple of potential issues with your solution. \Z matches trailing newlines, although not anything beyond that. (\z doesn't!) And \w equates to [a-zA-Z0-9_] as well, so you'll accidentally consider an underscore to be an alphanumeric character, which you might not want!

    alphanumeric?("_") # => true
    alphanumeric?("o1\n") # => true
    

    :-)

  • Default User Avatar

    It's leaning on functionality of Symbol#to_proc to do it. There's a few different pieces that make it up, and http://www.jacopretorius.net/2012/01/symbolto_proc-in-ruby.html seems to be a good in depth writeup of it.

    In short though:

    • &foo in arguments passes foo as a block to the method as if you'd done { foo.call }. If foo isn't a proc, it calls to_proc on it first.
    • Symbol#to_proc is implemented and creates a proc that sends the symbol to the block argument. That is :capitalize.to_proc returns a proc (block) equivilent to { |arg| arg.capitalize }
    • Putting it together means [].map { |element| element.capitalize } can be written as [].map(&:capitalize) and it returns the same result.