Skip to content

Comment on New to Ruby? Tips for the new Rubyist.parent

Comments

> "Array.new.methods - Object.methods" should read "Array.new.methods - Object.new.methods". Otherwise he's in fact subtracting the methods available on an error instance from the methods available on an instance of Class, not the methods available on a generic object..

You are wrong, classes are objects :)

    1.9.3p194 :084 > Object.new.methods - Object.methods
     => []

Actually, the author is inaccurate, comparing apples to oranges. Consider the opposite subtraction than you proposed:

    irb(0main):001:0> Object.methods - Object.new.methods
    => [:allocate, :new, ...]
That means doing "Array.new.methods - Object.methods" is subtracting things that may potentially be methods of an Array instance that should not be subtracted. It just so happens to work in this case because Array has none of those methods. But it doesn't work in the general case.

Consider for example, a "Person" class with a single instance method "name". In this case, "Person.new.methods - Object.methods" will not include "name", because this happens to be a method that applies both to Person instances and the object Object. The correct comparison is what vidarh said, "Array.new.methods - Object.new.methods".

The author's main point, however, was that you can write code like "Array.new.methods" and "Object.methods" at all, and that you can write code to subtract them. And this example shows that nicely.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.