Feature #9786

refinement & map(&:refinement_method) does not work

Added by Daniel Lo 11 months ago. Updated 11 months ago.

[ruby-core:62223]
Status:Rejected
Priority:Low
Assignee:-

Description

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:


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

Associated revisions

Revision 46731
Added by Usaku NAKAMURA 9 months ago

merge revision(s) 45845,45847: [Backport #9786]

* parse.y (local_tbl_gen): remove local variables duplicated with
  arguments.
   [Bug #9486]

History

#1 Updated by Nobuyoshi Nakada 11 months ago

  • Description updated (diff)
  • Status changed from Open to Rejected

Refinements does affects to the current scope only.
And Symbol#to_proc is independent from the callers' context.

A block works as you expect:

puts a.map {|x| x.add_z}

Also available in: Atom PDF