AnthonyW
AnthonyW

Reputation: 1998

Ruby reserved class method names

In Ruby, are there any methods that are reserved or have default meanings? I recently discovered that initialize is one. Are there any others that I should be aware of when naming my methods? (VI is not giving me the coloring clues that other IDEs give for reserved names.)

In particular, names that have meaning in other languages like run, main, toString, onExit, etc.

Upvotes: 0

Views: 1454

Answers (4)

PaReeOhNos
PaReeOhNos

Reputation: 4398

Also check out the list of reserver keywords here

If you're working in Rails, you may also want to take a look at this list

Upvotes: 2

Artur INTECH
Artur INTECH

Reputation: 7266

Despite nothing prevents declaring methods like public or private, I highly do not recommend doing so with any of the method names defined in core classes, such as Object and Module. Otherwise weird things can happen:

class Message
  def self.private
    puts 'private'
  end

  private
end

Message.private

Output:

private private

Private class method of a class Module was redefined as public.

Upvotes: 0

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70929

You can always see a list of the methods implemented by default for every class:

class Try
end

t = Try.new
puts t.methods.sort

EDIT: actually you may also want to look at the private methods (where initialize is):

puts t.private_methods.sort

Upvotes: 2

Arup Rakshit
Arup Rakshit

Reputation: 118261

you should be aware of those:

keywords

Upvotes: 0

Related Questions