5

The respond_to? method takes as argument, a method to be checked, but as a symbol.

Why as symbol? And how does ruby convert the method into a symbol?

3
  • it is not necessary that you pass method_name as symbol, you can also pass it as string. .respond_to?('method_name'). Commented May 9, 2017 at 8:08
  • Ok thanks. Because on codecadamy they said it takes a symbol as argument. So a string counts too ok. But why does it also take a symbol? What is this method doing with the symbol or the string (argument) ? Commented May 9, 2017 at 8:10
  • 2
    "on codecadamy they said" -- they can say whatever they want. To find the truth read the documentation. Commented May 9, 2017 at 8:17

1 Answer 1

7

There's no magic. Methods are attached to objects by their name, which is a symbol. In fact, Math.sin(2) is basically a shorthand for Math.send(:sin, 2) ("send message :sin to Math object, with parameter 2"). You can also access methods itself: Math.method(:sin) will give you the associated Method object (whose name, Math.method(:sin).name, is of course :sin), and Math.methods will list all implemented methods' names. Math.respond_to?(:sin) can basically be rewritten as Math.methods.include?(:sin) (this is simplified, as it ignores respond_to_missing?, but... close enough for this discussion).

Think of it this way. You go to a friend's house, and their mother answers the door. You ask, "Hello, is Tim here?" You don't need to actually drag your friend to the door and ask "Hello, is this individual here?" The name works just as well. :)

EDIT:

Hmmm that's confusing right now for me. What does named mean exactly? I mean maybe with a little example. I call it with array.each. When does the "name" :each come into play then?

I am not sure how to explain it better. Methods have names, just like people have names. When you say array.each, it is sending the message :each to the object that is contained in the variable array, pretty much exactly what array.send(:each) would do. Methods are pieces of code attached to objects via their names: when an object receives a message, it runs the piece of code that is associated with that message.

Specifically, in the standard Ruby implementation, objects of class Array, when they receive the message :each, will invoke the C function rb_ary_each that is defined in the Ruby source code, and linked to the message :each using rb_define_method(rb_cArray, "each", rb_ary_each, 0) (also in the Ruby source code).

Inside Ruby, there are basically two equivalent ways to define methods. These two are equivalent:

class Foo def bar puts "hello" end end class Foo define_method(:bar) do puts "hello" end end 

Both of them do the same thing: associate the message :bar with the piece of code do puts "hello" end. When :bar is received by a Foo (whether through Foo.send(:bar) or by Foo.bar), this piece of code is run.

Sign up to request clarification or add additional context in comments.

10 Comments

Ok thx for that explanation. That would mean that also for example .each from the Enumerable Class is also a Symbol :each ? Did I got this right?
does this api.rubyonrails.org/v4.2/classes/ActiveSupport/… play any role here (specially for symbol and string acceptance? or they are 2 different concepts?
No - the method each from Enumerable Module is named :each. Just like Tim (the individual) is not Tim (the name), but is called by that name.
@Md.FarhanMemon you linked to the documentation of Ruby on Rails. Rails is a framework over Ruby. The method respond_to? is a core Ruby method defined in class Object and it works with or without Rails.
@Md.FarhanMemon In a way, you're correct. Most method-handling functions will accept names as both strings and symbols. Math.method("sin") returns the same result as Math.method(:sin) does. Symbols and strings are different things as far as Ruby is concerned, but those functions handle both, indeed like HWIA does. But saying HWIA plays a role here because of this similar behaviour is like saying cars play a role in bank safes because they both use keys. :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.