6

I'm kind of confused about reserved words in Ruby.

"The Ruby Programming Language", co-authored by Matz, says that begin and end are reserved words of the language. They're certainly used syntactically to mark out blocks.

However, range objects in the language have methods named begin and end, as in

(1..10).end
=> 10 

Now, testing this out, I find that, indeed, I can define methods named "begin" and "end" on objects, though if I try to name a variable "begin" it fails. (Here's a sample of using it as a method name, it actually works...:)

class Foo
  def begin
    puts "hi"
  end
end

Foo.new.begin

So, I suppose I'm asking, what actually is the status of reserved words like this? I would have imagined that they couldn't be used for method names (and yet it seems to work) or that at the very least it would be terrible style (but it is actually used in the core language for the Range class).

I'm pretty confused as to when they're allowed to be used and for what. Is there even documentation on this?

2 Answers 2

13

Yes, they are reserved words. Yes, they can be used for method names. No, you can't call them without an explicit receiver. It's probably not a good idea anyway.

class Foo
  def if(foo)
    puts foo
  end
end

Foo.new.if("foo") # outputs foo, returns nil

Update: Here's a quote from "The Ruby Programming Language", by Matz (the creator of Ruby) himself:

In most languages, these words would be called “reserved words” and they would be never allowed as identifiers. The Ruby parser is flexible and does not complain if you prefix these keywords with @, @@, or $ prefixes and use them as instance, class, or global variable names. Also, you can use these keywords as method names, with the caveat that the method must always be explicitly invoked through an object.

9
  • Is this true for all "reserved words"? If it is bad style, why does the core API do it? (Though intuitively it seems like awful style -- indeed, an awful thing for the language to permit at all.) And, is this documented?
    – Perry
    Apr 6, 2012 at 17:12
  • It's permitted because sometimes having methods called like a reserved word may make sense (IMHO Range#end) is a good example. Things aren't always black and white, like most features this might be helpful when used with care. As for documentation, a quick Google search turned up tutorialspoint.com/ruby/ruby_syntax.htm, which states that "[t]hese reserved words may not be used as constant or variable names. They can, however, be used as method names", which IMHO makes perfect sense. Apr 6, 2012 at 17:15
  • That doesn't seem to be official documentation though. (I haven't found any mention about it in official documents, though that does not imply that it doesn't exist.)
    – Perry
    Apr 6, 2012 at 17:17
  • Ah, very cool. I don't suppose you could edit that to list the page it is on?
    – Perry
    Apr 6, 2012 at 17:38
  • (It appears to be on page 31...)
    – Perry
    Apr 6, 2012 at 17:43
2

When they are given in a form that is unambiguously a method call, you can use them. If you have a period in front of it .begin or have parentheses after is begin(), then it is unambiguously a method call. When you try to use it as a variable begin, it is ambiguous (in principle).

Actually, as Perry, notes, begin() might be tricky. I checked with irb with Ruby 1.9.3, and the following strange thing happens:

irb(main):001:0> def begin(foo)
irb(main):002:1> puts 'a'
irb(main):003:1> end
=> nil
irb(main):004:0> begin(3)
irb(main):005:1> 
irb(main):006:1* end
=> 3

It is not defined, and what looks like a method call might be just a block returning the last-evaluated 3. But the lines around def begin(foo) remains mystery.

4
  • 1
    Your claim about parentheses seems to be false, at least in Ruby 1.9.3 -- begin() does not seem to work for me.
    – Perry
    Apr 6, 2012 at 17:15
  • @sawa Nothing strange here. The parenthesis are interpreted as — unnecessary — parenthesis around the block's body. That's why the 3 gets returned as is. Wy the first definition is a mistery I don't understand, all method definitions return nil and self.methods.grep(/beg/) #=> [:begin]. If you just type send(:begin) in IRB, it will work. Apr 6, 2012 at 17:26
  • @MichaelKohl What is defined in the definition starting with def begin(foo)?
    – sawa
    Apr 6, 2012 at 17:28
  • @sawa: What's always defined when you define a method in IRB, an instance method of main, that's why self.methods will list it. Apr 6, 2012 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.