Surprising Extend

L

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?

Les
 
R

Robert Klemme

2008/5/29 Leslie Viljoen said:
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
 
L

Leslie Viljoen

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
 
D

David Masover

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

Leslie Viljoen

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
 
D

David Masover

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

Chris Shea

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
 
L

Leslie Viljoen

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
 
R

Robert Klemme

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
 
L

Leslie Viljoen

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top