Reputation: 1454
Trying to inject my own function (say check) to Float class. When I am doing from a module it is not injected to Float class. Please see the following snippets.
irb(main):001:0> module QA
irb(main):002:1> class Float
irb(main):003:2> def self.check
irb(main):004:3> end
irb(main):005:2> end
irb(main):006:1> end
=> nil
irb(main):007:0> include QA
=> Object
irb(main):008:0> Float.respond_to?(:check)
=> false
irb(main):009:0> extend QA
=> main
irb(main):010:0> Float.respond_to?(:check)
=> false
irb(main):011:0> class Float
irb(main):012:1> def self.check
irb(main):013:2> end
irb(main):014:1> end
=> nil
irb(main):015:0> Float.respond_to?(:check)
=> true
Upvotes: 1
Views: 72
Reputation: 369458
You have added the method to the QA::Float
class, not the Float
class. If you want to add the method to the Float
class, you should do
module QA
class ::Float
# stuff
end
end
Or even better just
class Float
# stuff
end
Upvotes: 2
Reputation: 434665
This:
module QA
class Float
def self.check
end
end
end
Is creating/modifying the class QA::Float
, not Float
. Try doing
QA::Float.respond_to?(:check)
And you'll see.
You could do something like this instead:
module QA
def self.included(klass)
Float.class_eval "def self.check;end"
end
end
include QA
Float.respond_to?(:check)
# true
Some useful references:
Upvotes: 2