[newbie] check class for method

I would like to check whether a class responds to an :include? method.
Checking the String class will result in true. The Fixnum to false.

I don’t know how to get the right syntax though.

irb> String::respond_to? :include?
=> true

irb> Fixnum::respond_to? :include?
=>true

irb> 1::respond_to? :include?
=> false

I probably misunderstand something… :frowning:

When you ask if something in ruby responds to some message you have to
keep in mind that instances of a class are objects and that classes are
also objects. The messages that the class object responds to may be
different from the messages that an instance of the class responds to.

Try this in irb:

1.class
=> Fixnum

1.methods.grep(/include/)
=> []

1.class.methods.grep(/include/)
=> [:included_modules, :include?, :include]

Compare these results to the syntax you are using:

Scott S. wrote in post #1169782:

When you ask if something in ruby responds to some message you have to
keep in mind that instances of a class are objects and that classes are
also objects. The messages that the class object responds to may be
different from the messages that an instance of the class responds to.

Try this in irb:

1.class
=> Fixnum

1.methods.grep(/include/)
=> []

1.class.methods.grep(/include/)
=> [:included_modules, :include?, :include]

Compare these results to the syntax you are using:

Thanks a lot Scott. Much appreciated. I’ll dive into this a bit more,
getting a grasp of this.

Explore the differences between dot notation and the “::” notation that
you were using. Hopefully that will help.

Scott S. wrote in post #1169844:

Explore the differences between dot notation and the “::” notation that
you were using. Hopefully that will help.

Thanks. I wasn’t aware of the difference. I am now :wink:

That’s good. To my thinking it’s better that you find out in your own
way rather than having someone just tell you, so I hope I managed to
help.

for your final exam: :wink:

::SOME_NAME

What does the “::” syntax with nothing before it do?

Scott S. wrote in post #1169955:

That’s good. To my thinking it’s better that you find out in your own
way rather than having someone just tell you, so I hope I managed to
help.

for your final exam: :wink:

::SOME_NAME

What does the “::” syntax with nothing before it do?

Thx, I like it… :wink:

The double colon operator is the scope resolution operator. It specifies
in which class or module you reference a constant. Preceding a constant
with :: means that you take it from the outer, or global, scope.

So, in other words, or better said, my own words, if you have a module
‘MyModule’ that contains a class ‘MyClass’, you can refer to it by
writing MyModule::MyClass. As if it were a path to a file in a
filesystem.
If ‘MyClass’ is a class in the outer (global) scope as well, you refer
to it like ::MyClass
This way you can refer to two different classes with the same name, but
each into their own namespace.

Hope I passed… :wink: