Project

General

Profile

Feature #9786

Updated by nobu (Nobuyoshi Nakada) over 10 years ago

Greetings! 

 Thank you for your hard work on ruby, I love the language. 

 I encountered an oddity, in which, I expected a refinement to be available and it wasn't. 

 Here is the sample code using: ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux] 

 ~~~ 
 $ irb 
 irb(main):001:0> module MyRefinement 
 irb(main):002:1>     refine Fixnum do 
 irb(main):003:2*       def add_z 
 irb(main):004:3>         self.to_s + 'z' 
 irb(main):005:3>       end 
 irb(main):006:2>     end 
 irb(main):007:1> end 
 => #<refinement:Fixnum@MyRefinement> 
 irb(main):008:0> 
 irb(main):009:0* 
 irb(main):010:0* class Test 
 irb(main):011:1>     using MyRefinement 
 irb(main):012:1> 
 irb(main):013:1*     a = [ 1, 2 ] 
 irb(main):014:1>     puts a.map(&:to_s) 
 irb(main):015:1>     puts '-----' 
 irb(main):016:1>     puts a.first.add_z 
 irb(main):017:1>     puts '-----' 
 irb(main):018:1>     puts a.map(&:add_z) 
 irb(main):019:1> end 
 1 
 2 
 ----- 
 1z 
 ----- 
 NoMethodError: super: no superclass method `add_z' for 1:Fixnum 
         from (irb):18:in `map' 
         from (irb):18:in `<class:Test>' 
         from (irb):10 
         from /usr/local/bin/irb:11:in `<main>' 
 irb(main):020:0> 
 ~~~ 

 Here is the code without the irb comments: 

 ~~~ruby ~~~ 

 module MyRefinement 
   refine Fixnum do 
     def add_z 
       self.to_s + 'z' 
     end 
   end 
 end 

 class Test 
   using MyRefinement 

   a = [ 1, 2 ] 
   puts a.map(&:to_s) 
   puts '-----' 
   puts a.first.add_z 
   puts '-----' 
   puts a.map(&:add_z) 
 end 
 ~~~ 

 Thank you! 

 -daniel 

Back