[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Surprising Extend

Leslie Viljoen

5/29/2008 3:22:00 PM

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

12 Answers

Robert Klemme

5/29/2008 3:26:00 PM

0

2008/5/29 Leslie Viljoen <leslieviljoen@gmail.com>:
> 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

Leslie Viljoen

5/29/2008 3:36:00 PM

0

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

David Masover

5/29/2008 3:45:00 PM

0

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.

Leslie Viljoen

5/29/2008 3:57:00 PM

0

On Thu, May 29, 2008 at 5:45 PM, David Masover <ninja@slaphack.com> 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

David Masover

5/29/2008 4:22:00 PM

0

On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
> On Thu, May 29, 2008 at 5:45 PM, David Masover <ninja@slaphack.com> 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.

Chris Shea

5/29/2008 4:33:00 PM

0

On May 29, 9:21 am, Leslie Viljoen <leslievilj...@gmail.com> 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/class...
extend: http://whytheluckystiff.net/ruby/pickaxe/html/class...

HTH,
Chris

Leslie Viljoen

5/29/2008 7:02:00 PM

0

On Thu, May 29, 2008 at 6:22 PM, David Masover <ninja@slaphack.com> wrote:
> On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
>> On Thu, May 29, 2008 at 5:45 PM, David Masover <ninja@slaphack.com> 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

Robert Klemme

5/29/2008 8:18:00 PM

0

On 29.05.2008 21:02, Leslie Viljoen wrote:
> On Thu, May 29, 2008 at 6:22 PM, David Masover <ninja@slaphack.com> wrote:
>> On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
>>> On Thu, May 29, 2008 at 5:45 PM, David Masover <ninja@slaphack.com> 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

Leslie Viljoen

5/29/2008 8:47:00 PM

0

On Thu, May 29, 2008 at 10:20 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On 29.05.2008 21:02, Leslie Viljoen wrote:
>>
>> On Thu, May 29, 2008 at 6:22 PM, David Masover <ninja@slaphack.com> wrote:
>>>
>>> On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
>>>>
>>>> On Thu, May 29, 2008 at 5:45 PM, David Masover <ninja@slaphack.com>
>>>> 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!

Robert Klemme

5/30/2008 6:21:00 AM

0

On 29.05.2008 22:47, Leslie Viljoen wrote:
> On Thu, May 29, 2008 at 10:20 PM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>> On 29.05.2008 21:02, Leslie Viljoen wrote:
>>> On Thu, May 29, 2008 at 6:22 PM, David Masover <ninja@slaphack.com> wrote:
>>>> On Thursday 29 May 2008 10:56:47 Leslie Viljoen wrote:
>>>>> On Thu, May 29, 2008 at 5:45 PM, David Masover <ninja@slaphack.com>
>>>>> 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