Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby method call with a symbol parameter with no space

Tags:

ruby

I saw something in a Rails app I am reviewing that I expected to be an error, however it is working. I am confused by the way this method is being called with this parameter. No spaces, just two words separated by a colon:

    ree-1.8.7-2012.02 :001 > def muffin(x)
    ree-1.8.7-2012.02 :002?>   puts x.inspect
    ree-1.8.7-2012.02 :003?>   end
    => nil
    ree-1.8.7-2012.02 :004 > muffin:tuffin
    :tuffin

Also works with a string:

    ree-1.8.7-2012.02 :004 > muffin'd'
    "d"

Is this the expected and correct thing?

like image 534
user3259018 Avatar asked Mar 21 '23 17:03

user3259018


1 Answers

Ruby enjoys one of the most relaxed grammars in the industry. Yes, the space is optional. And try muffin(:tuffin) if you want your colleagues to see what's going on.

Furtherless, you can write:

def muffin x

Yet another example of "because I can" isn't a valid reason for doing something. C-;

like image 64
Phlip Avatar answered Mar 31 '23 21:03

Phlip