Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Surprising Extend

Reply
Thread Tools

Surprising Extend

 
 
Leslie Viljoen
Guest
Posts: n/a
 
      05-29-2008
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

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      05-29-2008
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

 
Reply With Quote
 
 
 
 
Leslie Viljoen
Guest
Posts: n/a
 
      05-29-2008
>
> 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

 
Reply With Quote
 
David Masover
Guest
Posts: n/a
 
      05-29-2008
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.

 
Reply With Quote
 
Leslie Viljoen
Guest
Posts: n/a
 
      05-29-2008
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

 
Reply With Quote
 
David Masover
Guest
Posts: n/a
 
      05-29-2008
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.

 
Reply With Quote
 
Chris Shea
Guest
Posts: n/a
 
      05-29-2008
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/pic...lasses.html#UC
extend: http://whytheluckystiff.net/ruby/pic...lasses.html#UD

HTH,
Chris
 
Reply With Quote
 
Leslie Viljoen
Guest
Posts: n/a
 
      05-29-2008
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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-29-2008
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
 
Reply With Quote
 
Leslie Viljoen
Guest
Posts: n/a
 
      05-29-2008
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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Surprising Partial Specialization Michael Olea C++ 1 06-14-2005 03:57 PM
It is very surprising .... janek C++ 3 02-21-2005 06:44 PM
Help with Datagrid (Surprising No Data!!) naija naija ASP .Net 2 10-02-2004 04:10 AM
RE: Integer division, surprising results Tim Peters Python 1 05-12-2004 11:54 AM
Integer division, surprising results Michael Cornelius Python 2 05-12-2004 03:52 AM



Advertisments