Surprising Extend

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

2008/5/29 Leslie V. [email protected]:

           unpack("C*").map{|b| b.to_s(16)}.join(" ")

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

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

On Thursday 29 May 2008 10:36:27 Leslie V. wrote:

----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.

On Thursday 29 May 2008 10:56:47 Leslie V. wrote:

On Thu, May 29, 2008 at 5:45 PM, David M. [email protected] 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.

On Thu, May 29, 2008 at 5:45 PM, David M. [email protected]
wrote:

end
method has probably become a class method for String? As in, String.to_hex?

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

On May 29, 9:21 am, Leslie V. [email protected] wrote:

            unpack("C*").map{|b| b.to_s(16)}.join(" ")

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

On 29.05.2008 21:02, Leslie V. wrote:

Yes, that’s what I said!
Actually, no (see above). :slight_smile:

robert

On Thu, May 29, 2008 at 6:22 PM, David M. [email protected]
wrote:

the file.
Yes, that’s what I said!

Les

On Thu, May 29, 2008 at 10:20 PM, Robert K.
[email protected] wrote:

Actually, no (see above). :slight_smile:
My response was “Oh ok.”
What a silly argument!

On Fri, May 30, 2008 at 8:24 AM, Robert K.
[email protected] wrote:

On Thursday 29 May 2008 10:56:47 Leslie V. wrote:
of
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. :slight_smile:

And Chunky bacon to you sir!

On 29.05.2008 22:47, Leslie V. wrote:

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). :slight_smile:

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. :slight_smile:

Kind regards

robert

On Fri, May 30, 2008 at 8:24 AM, Robert K.
[email protected] wrote:

As far as I know the charter of the group does not prohibit silliness.
Sometimes the most fun comes from silliness. :slight_smile:
The only reason I am here :wink:
Robert