Ruby/Tk Primer: Creating a cron GUI Interface with Ruby/Tk
Subject:   Answer to the self.puts dilema
Date:   2004-07-19 16:12:40
From:   Merc
Response to: Answer to the self.puts dilema

Just a style note. Unlike Python, Ruby doesn't require parentheses around method calls that take no arguments (in fact, under most circumstances, it doesn't require them around methods that take arguments). That's why you can say puts "Hello World!", rather than puts("Hello World!").

Most Ruby programmers omit them when they're not necessary, so they would write that code:

class MyClass
def sayHello()

def hello
puts "Hello, World!!!"

myClass =

Part of the reason this is significant is that it allows you to treat functions as if they were attributes:

class ComplexNumber
attr_accessor :real, :complex

n =
n.real = 5
n.complex = 6

puts "(#{n.real}, #{n.complex})"

n.real, and n.real= are actually method calls, but by omitting parentheses they look more natural.

Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Answer to the self.puts dilema
    2004-07-19 20:30:53  Christopher Roach | O'Reilly Blogger [View]

    You're absolutely correct — you do not need the parentheses in a Ruby method call. My use of the parentheses in the example was simply because I am so used to using them in C/C++ and Java in my daily work.

    I did try to mention that Ruby does not need the parentheses (towards the middle of the third section), but I didn't really discuss any of the advantages of this property of the language. Therefore, I am glad that you pointed out that one of the advantages of not requiring the parentheses is Ruby's ability to treat a method like an attribute. Thus, setter and getter methods look just like direct attribute manipulation, giving the user the illusion that they are accessing data members directly while still following the OO concepts of data abstraction and encapsulation.

    Thanks for the example.
  • Answer to the self.puts dilema
    2004-07-19 16:18:43  Merc [View]

    Oops, forgot to remove the parentheses around sayHello() and hello()