Evan Martin (evan) wrote in evan_tech,
Evan Martin
evan
evan_tech

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)'
2:3:4
Subscribe
  • Post a new comment

    Error

    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.
  • 9 comments