From: Leslie Viljoen Date: 2008-05-30T00:56:47+09:00 Subject: Re: Surprising Extend On Thu, May 29, 2008 at 5:45 PM, David Masover wrote: > On Thursday 29 May 2008 10:36:27 Leslie Viljoen wrote: >> > >> > Can you show some real code and real error messages? Normally there >> > should not be an issue with your first approach. >> ----In StringExt.rb: >> >> module StringExt >> def to_hex >> unpack("C*").map{|b| b.to_s(16)}.join(" ") >> end >> end >> >> ----In use.rb: >> >> require 'StringExt' >> >> String.extend StringExt >> s = "anystringwilldo" >> puts s.to_hex > > I'm not sure, but considering that you can extend individual instances, that > method has probably become a class method for String? As in, String.to_hex? > > But your _first_ approach should work. In StringExt.rb: > > class String > def to_hex > unpack("C*").map{|b| b.to_s(16)}.join(" ") > end > end > > In use.rb: > > require 'StringExt' > s = 'anystringwilldo' > puts s.to_hex > > For me, running use.rb outputs > > 61 6e 79 73 74 72 69 6e 67 77 69 6c 6c 64 6f > > tested in both Ruby 1.8 and Ruby 1.9. Oh ok. And then I can use 'extend' when I want to extend an instance. Thanks! Les