[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Overloading to_s in a class of my own

carl

10/25/2003 8:31:00 PM

I'm confused by a problem I'm having trying to overload the to_s
operator so that my class will be accepted as a string where
applicable. Here's a simplified version of the class:

class Path
def initialize(pathstring)
@pathstring = pathstring
end

def to_s
@pathstring
end
end

It seems to work when I pass an instance to puts or something like
that. Why won't it work when I do something like this:

p = Path.new("somepath")
Dir.mkdir(p)

I get the following error message:

test.rb:4:in `mkdir': wrong argument type Path (expected String)
(TypeError)
from test.rb:4

Thanks,

Carl
6 Answers

Florian Gross

10/25/2003 9:21:00 PM

0

Moin!

Carl Youngblood wrote:

> I'm confused by a problem I'm having trying to overload the to_s
> operator so that my class will be accepted as a string where
> applicable. Here's a simplified version of the class:

You're confusing #to_s with #to_str.

> Thanks,
> Carl

Regards,
Florian Gross


Carl Youngblood

10/25/2003 9:23:00 PM

0

I guess this isn't exactly "overloading," since there is no original
method to overload. Sorry for the confusion. Whatever it is, I think
you guys know what I'm trying to say.

Carl

Carl Youngblood wrote:
> I'm confused by a problem I'm having trying to overload the to_s
> operator so that my class will be accepted as a string where
> applicable. Here's a simplified version of the class:
>
> class Path
> def initialize(pathstring)
> @pathstring = pathstring
> end
>
> def to_s
> @pathstring
> end
> end
>
> It seems to work when I pass an instance to puts or something like
> that. Why won't it work when I do something like this:
>
> p = Path.new("somepath")
> Dir.mkdir(p)
>
> I get the following error message:
>
> test.rb:4:in `mkdir': wrong argument type Path (expected String)
> (TypeError)
> from test.rb:4
>
> Thanks,
>
> Carl

Carl Youngblood

10/25/2003 9:45:00 PM

0

Creating to_str in my class still doesn't work.

Florian Gross wrote:
> Moin!
>
> Carl Youngblood wrote:
>
>> I'm confused by a problem I'm having trying to overload the to_s
>> operator so that my class will be accepted as a string where
>> applicable. Here's a simplified version of the class:
>
>
> You're confusing #to_s with #to_str.
>
>> Thanks, Carl
>
>
> Regards,
> Florian Gross
>
>

Florian Gross

10/25/2003 11:24:00 PM

0

Moin!

Carl Youngblood wrote:

> Creating to_str in my class still doesn't work.

This works fine for me:

class Path
def initialize(path)
@path = path.to_s
end

def to_str
@path
end
end

Dir.mkdir(Path.new("Hello World!"))

>> Regards,
>> Florian Gross



Carl Youngblood

10/26/2003 3:20:00 AM

0

You're right. I just tested it on my own box, which runs Ruby 1.8.0.
Unfortunately, the lab machines in my university's computer science
department are the things we are running this code on, and they are
running ruby 1.6.8 (2002-12-24) [i386-linux-gnu]. The sysadmins don't
want to upgrade to 1.8.0 because a lot of other packages would need to
be upgraded as well. There's nothing more frustrating than not being
able to take advantage of improvements in software just because
somebody's running an older version!

Thanks for your help,

Carl

Florian Gross wrote:
> Moin!
>
> Carl Youngblood wrote:
>
>> Creating to_str in my class still doesn't work.
>
>
> This works fine for me:
>
> class Path
> def initialize(path)
> @path = path.to_s
> end
>
> def to_str
> @path
> end
> end
>
> Dir.mkdir(Path.new("Hello World!"))
>
>>> Regards,
>>> Florian Gross
>
>
>
>

Robert Klemme

10/27/2003 8:55:00 AM

0


"Carl Youngblood" <carl@ycs.biz> schrieb im Newsbeitrag
news:93Hmb.31810$HS4.118694@attbi_s01...
> You're right. I just tested it on my own box, which runs Ruby 1.8.0.
> Unfortunately, the lab machines in my university's computer science
> department are the things we are running this code on, and they are
> running ruby 1.6.8 (2002-12-24) [i386-linux-gnu]. The sysadmins don't
> want to upgrade to 1.8.0 because a lot of other packages would need to
> be upgraded as well. There's nothing more frustrating than not being
> able to take advantage of improvements in software just because
> somebody's running an older version!

That's easily fixed in your case:

class Dir
class << self
alias __mkdir__ mkdir

def mkdir(dir)
__mkdir__(dir.to_s)
end
end
end

However, I wouldn't regard it as too big an disadvantage if you had to do
Dir.mkdir( path.to_s ). Another option is to let Path inherit String:

class Path < String
def initialize(pathstring)
super(pathstring)
end
end

Or use String directly, since you can always manipulate file name strings
with File.split, File.join, File.dirname, File.basename, File.expand_path
and paths with String.split(File::PATH_SEPARATOR).

Kind regards

robert