Evan Martin (evan) wrote in evan_tech,
Evan Martin

ruby wart

I'm still a huge Ruby fan, and I really do like the way that you call argumentless functions without parens and the way that affects the rest of the language.

But the way procs and blocks are different is lame:
["a", "b c"].map { |x| CGI.escape(x) }
=> ["a", "b+c"]  # the result i want

["a", "b c"].map(CGI.escape)
ArgumentError: wrong number of arguments (0 for 1)

p = Proc.new { |x| CGI.escape(x) }
["a", "b c"].map(&p)
=> ["a", "b+c"]

["a", "b c"].map(&CGI.escape)
ArgumentError: wrong number of arguments (0 for 1)

I run into a similar pattern all the time writing Python at work and it's equally lame:
>>> map(upper, ['abc', 'def'])  # i know this won't work
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'upper' is not defined
>>> map(lambda x: x.upper(), ['abc', 'def'])
['ABC', 'DEF']

So my code ends up littered with more readable but still lame [x.foo() for x in list] over and over.

Perl worked around it with the implicit variable, which makes the language-purity-types shudder but I think is a pretty clever way to design it.
% perl -e '$,=":"; print map { $_ + 1 } (1,2,3)'

  • livejournal kids

    Neat image from Jack Dorsey. Every so often someone will ask me about Twitter and I'll dig up a a random day from Brad's LJ in 1999 and talk about…

  • ljrb release 0.3.1

    LiveJournal Ruby module update: This release won't die when the "useragent" property is present in an entry. I've also added support for passing…

  • ljrb 0.3.0

    ljrb 0.3.0: This release adds support for the "current_location" field and fetching friendofs in the same request as fetching friends. There's also…

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.