Surprising Extend

Discussion in 'Ruby' started by Leslie Viljoen, May 29, 2008.

  1. Hi people

    I want to make a library which can extend the String class. I don't want to
    keep putting the class String; def blah; end; end; at the top of all my
    files, I just want to require 'StringExt'

    This doesn't work, after I have 'require'd it into my file:

    class String
    def to_hex
    unpack("C*").map{|b| b.to_s(16)}.join(" ")
    end
    end

    So I tried this:

    module StringExt
    def to_hex
    unpack("C*").map{|b| b.to_s(16)}.join(" ")
    end
    end
    ----
    then in my file I put: require 'StringExt'; String.extend StringExt
    ..but that didn't work.

    Then I tried:
    s = "sdfdsfsdfsdF"
    s.extend StringExt
    s.to_hex

    ...but that didn't work.

    In the Pragmatic Programmer there's an example, but the module is
    defined in the same file as where it's used - and only the s.extend...
    version then works.

    I really want String.extend, to change all strings. How do a 'require'
    a file that then changes the String class?

    Les
     
    Leslie Viljoen, May 29, 2008
    #1
    1. Advertising

  2. 2008/5/29 Leslie Viljoen <>:
    > Hi people
    >
    > I want to make a library which can extend the String class. I don't want to
    > keep putting the class String; def blah; end; end; at the top of all my
    > files, I just want to require 'StringExt'
    >
    > This doesn't work, after I have 'require'd it into my file:
    >
    > class String
    > def to_hex
    > unpack("C*").map{|b| b.to_s(16)}.join(" ")
    > end
    > end
    >
    > So I tried this:
    >
    > module StringExt
    > def to_hex
    > unpack("C*").map{|b| b.to_s(16)}.join(" ")
    > end
    > end
    > ----
    > then in my file I put: require 'StringExt'; String.extend StringExt
    > ..but that didn't work.
    >
    > Then I tried:
    > s = "sdfdsfsdfsdF"
    > s.extend StringExt
    > s.to_hex
    >
    > ...but that didn't work.
    >
    > In the Pragmatic Programmer there's an example, but the module is
    > defined in the same file as where it's used - and only the s.extend...
    > version then works.
    >
    > I really want String.extend, to change all strings. How do a 'require'
    > a file that then changes the String class?


    Can you show some real code and real error messages? Normally there
    should not be an issue with your first approach.

    Cheers

    robert



    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, May 29, 2008
    #2
    1. Advertising

  3. >
    > Can you show some real code and real error messages? Normally there
    > should not be an issue with your first approach.


    Ah, I was making DOS files on Linux. That thwarted my modulating.

    But I can still only extend String instances (s.extend) - I want to extend the
    String class. Here's my attempt:

    ----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 get "undefined method to_hex" for my string.


    Thanks!
    Les
     
    Leslie Viljoen, May 29, 2008
    #3
  4. 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.
     
    David Masover, May 29, 2008
    #4
  5. 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
     
    Leslie Viljoen, May 29, 2008
    #5
  6. On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
    > On Thu, May 29, 2008 at 5:45 PM, David Masover <> wrote:
    > >
    > > But your _first_ approach should work. In StringExt.rb:

    >
    > Oh ok. And then I can use 'extend' when I want to extend an instance.
    > Thanks!


    actually, no. What I wrote modifies the String class, so all instances of
    String have a working to_hex. You don't have to do anything more than require
    the file.
     
    David Masover, May 29, 2008
    #6
  7. Leslie Viljoen

    Chris Shea Guest

    On May 29, 9:21 am, Leslie Viljoen <> wrote:
    > Hi people
    >
    > I want to make a library which can extend the String class. I don't want to
    > keep putting the class String; def blah; end; end; at the top of all my
    > files, I just want to require 'StringExt'
    >
    > This doesn't work, after I have 'require'd it into my file:
    >
    > class String
    > def to_hex
    > unpack("C*").map{|b| b.to_s(16)}.join(" ")
    > end
    > end
    >
    > So I tried this:
    >
    > module StringExt
    > def to_hex
    > unpack("C*").map{|b| b.to_s(16)}.join(" ")
    > end
    > end
    > ----
    > then in my file I put: require 'StringExt'; String.extend StringExt
    > .but that didn't work.
    >
    > Then I tried:
    > s = "sdfdsfsdfsdF"
    > s.extend StringExt
    > s.to_hex
    >
    > ..but that didn't work.
    >
    > In the Pragmatic Programmer there's an example, but the module is
    > defined in the same file as where it's used - and only the s.extend...
    > version then works.
    >
    > I really want String.extend, to change all strings. How do a 'require'
    > a file that then changes the String class?
    >
    > Les


    It sounds like what you want is include, not extend. If your goal is
    to add instance methods to all String objects, you can define your
    methods in StringExt, and then:

    class String
    include StringExt
    end

    In the online first edition of the Pickaxe:

    include: http://whytheluckystiff.net/ruby/pickaxe/html/classes.html#UC
    extend: http://whytheluckystiff.net/ruby/pickaxe/html/classes.html#UD

    HTH,
    Chris
     
    Chris Shea, May 29, 2008
    #7
  8. On Thu, May 29, 2008 at 6:22 PM, David Masover <> wrote:
    > On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
    >> On Thu, May 29, 2008 at 5:45 PM, David Masover <> wrote:
    >> >
    >> > But your _first_ approach should work. In StringExt.rb:

    >>
    >> Oh ok. And then I can use 'extend' when I want to extend an instance.
    >> Thanks!

    >
    > actually, no. What I wrote modifies the String class, so all instances of
    > String have a working to_hex. You don't have to do anything more than require
    > the file.


    Yes, that's what I said!

    Les
     
    Leslie Viljoen, May 29, 2008
    #8
  9. On 29.05.2008 21:02, Leslie Viljoen wrote:
    > On Thu, May 29, 2008 at 6:22 PM, David Masover <> wrote:
    >> On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
    >>> On Thu, May 29, 2008 at 5:45 PM, David Masover <> wrote:
    >>>> But your _first_ approach should work. In StringExt.rb:
    >>> Oh ok. And then I can use 'extend' when I want to extend an instance.
    >>> Thanks!

    >> actually, no. What I wrote modifies the String class, so all instances of
    >> String have a working to_hex. You don't have to do anything more than require
    >> the file.

    >
    > Yes, that's what I said!


    Actually, no (see above). :)

    robert
     
    Robert Klemme, May 29, 2008
    #9
  10. On Thu, May 29, 2008 at 10:20 PM, Robert Klemme
    <> wrote:
    > On 29.05.2008 21:02, Leslie Viljoen wrote:
    >>
    >> On Thu, May 29, 2008 at 6:22 PM, David Masover <> wrote:
    >>>
    >>> On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
    >>>>
    >>>> On Thu, May 29, 2008 at 5:45 PM, David Masover <>
    >>>> wrote:
    >>>>>
    >>>>> But your _first_ approach should work. In StringExt.rb:
    >>>>
    >>>> Oh ok. And then I can use 'extend' when I want to extend an instance.
    >>>> Thanks!
    >>>
    >>> actually, no. What I wrote modifies the String class, so all instances of
    >>> String have a working to_hex. You don't have to do anything more than
    >>> require
    >>> the file.

    >>
    >> Yes, that's what I said!

    >
    > Actually, no (see above). :)


    My response was "Oh ok."
    What a silly argument!
     
    Leslie Viljoen, May 29, 2008
    #10
  11. On 29.05.2008 22:47, Leslie Viljoen wrote:
    > On Thu, May 29, 2008 at 10:20 PM, Robert Klemme
    > <> wrote:
    >> On 29.05.2008 21:02, Leslie Viljoen wrote:
    >>> On Thu, May 29, 2008 at 6:22 PM, David Masover <> wrote:
    >>>> On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
    >>>>> On Thu, May 29, 2008 at 5:45 PM, David Masover <>
    >>>>> wrote:
    >>>>>> But your _first_ approach should work. In StringExt.rb:
    >>>>> Oh ok. And then I can use 'extend' when I want to extend an instance.
    >>>>> Thanks!
    >>>> actually, no. What I wrote modifies the String class, so all instances of
    >>>> String have a working to_hex. You don't have to do anything more than
    >>>> require
    >>>> the file.
    >>> Yes, that's what I said!

    >> Actually, no (see above). :)

    >
    > My response was "Oh ok."


    And you continued "and _then_ I can use extend when I wan to extend an
    instance" - but if the presented approach was followed there was no
    module you could use for extend.

    Maybe you meant "and I have to define a module and can use extend when I
    wan to extend an instance" but that was not at all clear (at least to me).

    > What a silly argument!


    As far as I know the charter of the group does not prohibit silliness.
    Sometimes the most fun comes from silliness. :)

    Kind regards

    robert
     
    Robert Klemme, May 30, 2008
    #11
  12. Leslie Viljoen

    Robert Dober Guest

    On Fri, May 30, 2008 at 8:24 AM, Robert Klemme
    <> wrote:

    >
    > As far as I know the charter of the group does not prohibit silliness.
    > Sometimes the most fun comes from silliness. :)

    The only reason I am here ;)
    Robert
     
    Robert Dober, May 30, 2008
    #12
  13. On Fri, May 30, 2008 at 8:24 AM, Robert Klemme
    <> wrote:
    > On 29.05.2008 22:47, Leslie Viljoen wrote:
    >>
    >> On Thu, May 29, 2008 at 10:20 PM, Robert Klemme
    >> <> wrote:
    >>>
    >>> On 29.05.2008 21:02, Leslie Viljoen wrote:
    >>>>
    >>>> On Thu, May 29, 2008 at 6:22 PM, David Masover <>
    >>>> wrote:
    >>>>>
    >>>>> On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
    >>>>>>
    >>>>>> On Thu, May 29, 2008 at 5:45 PM, David Masover <>
    >>>>>> wrote:
    >>>>>>>
    >>>>>>> But your _first_ approach should work. In StringExt.rb:
    >>>>>>
    >>>>>> Oh ok. And then I can use 'extend' when I want to extend an instance.
    >>>>>> Thanks!
    >>>>>
    >>>>> actually, no. What I wrote modifies the String class, so all instances
    >>>>> of
    >>>>> String have a working to_hex. You don't have to do anything more than
    >>>>> require
    >>>>> the file.
    >>>>
    >>>> Yes, that's what I said!
    >>>
    >>> Actually, no (see above). :)

    >>
    >> My response was "Oh ok."

    >
    > And you continued "and _then_ I can use extend when I wan to extend an
    > instance" - but if the presented approach was followed there was no module
    > you could use for extend.
    >
    > Maybe you meant "and I have to define a module and can use extend when I wan
    > to extend an instance" but that was not at all clear (at least to me).


    Oh I see, thanks for that.

    >> What a silly argument!

    >
    > As far as I know the charter of the group does not prohibit silliness.
    > Sometimes the most fun comes from silliness. :)


    And Chunky bacon to you sir!
     
    Leslie Viljoen, May 30, 2008
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. naija naija

    Help with Datagrid (Surprising No Data!!)

    naija naija, Sep 30, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    437
    naijacoder naijacoder
    Oct 2, 2004
  2. janek
    Replies:
    3
    Views:
    424
    Ioannis Vranos
    Feb 21, 2005
  3. Michael Olea

    A Surprising Partial Specialization

    Michael Olea, Jun 14, 2005, in forum: C++
    Replies:
    1
    Views:
    320
    Pete Becker
    Jun 14, 2005
  4. Michael Cornelius

    Integer division, surprising results

    Michael Cornelius, May 10, 2004, in forum: Python
    Replies:
    2
    Views:
    366
    Steve
    May 12, 2004
  5. Tim Peters
    Replies:
    1
    Views:
    363
    Ralf Muschall
    May 12, 2004
Loading...

Share This Page