0
Fixnum.methods.sort
=> [:!, :!=, :!~, :<, :<=, :<=>, ..., :trust, :untaint, :untrust, :untrusted?]

Why doesn't it display :*, :/, :+, :-, :% (or :"*", ":/", etc.) as methods?

I see that they are considered methods.

kubasub
  • 455
  • 1
  • 5
  • 12
  • `1.methods`, `Fixnum` is a class. – Victor Moroz Aug 01 '14 at 00:33
  • 7
    Try `Fixnum.instance_methods.sort`. – Cary Swoveland Aug 01 '14 at 00:33
  • Metaprogramming? Hmmm – Cary Swoveland Aug 01 '14 at 00:37
  • @CarySwoveland, isn't reflection a type of metaprogramming? [Some think so](http://stackoverflow.com/questions/7641881/is-metaprogramming-a-subset-of-reflection). – kubasub Aug 01 '14 at 11:56
  • `methods` and `instance_methods` are just [garden-variety](http://i.word.com/idictionary/garden-variety) Ruby methods. I don't see what that has to do with reflection. More to the point, I don't think most members searching on "Ruby" and "metaprogramming" would expect a question like yours to pop up. Consider also that yor question will be invisible to any member who filters on the "metaprogramming" tag (i.e., "no metaprogramming questions, please"). – Cary Swoveland Aug 01 '14 at 15:23
  • @CarySwoveland, Garden-variety or not, they still use [reflection](http://en.wikipedia.org/wiki/Reflection_(computer_programming)) as I understand it (introspection at runtime). Practically, you're right about the SO filtering. I'll keep that in mind for next time. – kubasub Aug 01 '14 at 16:07
  • 1
    I personally think metaprogramming is a fine tag for this question – JKillian Aug 01 '14 at 16:10

2 Answers2

1

Fixnum is an instance of Class. Class doesn't define a * instance method (what would that even do), nor do Class's ancestors (Module, Object, Kernel, BasicObject).

Now, 1 on the other hand is an instance of Fixnum, and since Fixnum defines a * instance method, that instance method shows up when you ask 1 about its methods:

1.methods.sort
# => [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, … ]

You can see that Fixnum defines an instance method named *:

Fixnum.instance_methods.sort
# => [:!, :!=, :!~, :%, :&, :*, :**, :+, :+@, :-, :-@, :/, :<, :<<, :<=, … ]
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • thank you! I initially tried `Fixnum.new.methods`, but since new is not defined on fixnum, I guess I gave up on that right there. – kubasub Aug 01 '14 at 11:52
-1

Because they are not class methods of Fixnum.

sawa
  • 165,429
  • 45
  • 277
  • 381