ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  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()
self.hello()
end


private
def hello
puts "Hello, World!!!"
end
end


myClass = MyClass.new
myClass.sayHello


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
end


n = ComplexNumber.new
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.


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

  2. Answer to the self.puts dilema
    2004-07-19 16:18:43  Merc [View]

1 to 2 of 2