0

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.

4 Answers 4

2

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
Sign up to request clarification or add additional context in comments.

4 Comments

It's not quite that simple :) Basically you can override every method in Ruby, but you shouldn't, unless you know what you are doing (ie. Meta-programming).
t.methods did not return anything but I assume it would if I addded anything to the class. t.private_methods was very informative however. Thanks!
@jbr Accidental overrides are exactly what I am attempting to avoid.
@jbr of course you can. You can do almost anything in ruby, but from the question I got OP wanted a list of methods already present.
2

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

Comments

0

you should be aware of those:

keywords

Comments

0

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.