To Writing Functions in Ruby w/o Parentheses

November 24, 2006

by Sibers CTO Andrey Gavrilov

One of the requirements to scripting languages is an opportunity to call a function without using parentheses (e.g., print “test” instead of print (“test”)). The problems arising out of this are obvious as construction func1 func2 “test” with variable parameters can be treated either as

func1(func2("test")) or as func1(func2(),"test") .

Ruby solves the problem by informing about errors just noticing a slightest ambiguity:

def func1(name)
puts name
end

def func2(name)
return name
end


func1 func2 "test"
-:9: warning: parenthesize argument(s) for future version

def func1(name1, name2)
puts name1 + name2
end

def func2()
return "test1"
end

func1 func2 "test2"
-:19: warning: parenthesize argument(s) for future version

It is absolutely right of Ruby to do so as otherwise adding an optional parameter to the function could cause errors in its calling. It looks, though, as a patch to potentially holed syntax.
The most logical solution seems to use only one argument in the form without parentheses. Anyway, using such a form for a few arguments doesn’t improve readability. In such a case form func1 func2 "test" is interpreted only as func1(func2("test")) . The solution is strict but lacks logic as well.
If I wrote a scripting language:
•Form func arg with "arg" being one argument would be the only form to call the function.
•Form ("a", "b", name:"c", "d") would be vocabulary builder

[0] => "a",
[1] => "b",
["name"] => "c",
[4] => "d",

•So when calling a function, it uses its argument as vocabulary, i.e. it tries to fill its arguments from the vocabulary first by name, then by the serial number and finally by the default value. If some of the arguments are left empty, an error is reported.
•To make constructions of the form print "test" work, I’d define property [0] in the class Object, which would point the object itself by default. Thus, when calling a function with one argument without parentheses, it would refer to the object as vocabulary and get it as the first argument (provided [0] is not redefined as in the case with list types). If a function has two arguments and [1] or a property with the name of the argument is not additionally defined, an error is reported.

2 Responses to “To Writing Functions in Ruby w/o Parentheses”

  1. Idetrorce Says:

    very interesting, but I don’t agree with you
    Idetrorce


  2. Spot on with this write-up, I definitely think this website needs very much far more consideration. I’ll most likely be again to read significantly more, thanks for that information.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.