Overloading to_s in a class of my own

C

Carl Youngblood

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
 
F

Florian Gross

Moin!

Carl said:
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
 
C

Carl Youngblood

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
 
F

Florian Gross

Moin!

Carl said:
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!"))
 
C

Carl Youngblood

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
 
R

Robert Klemme

Carl Youngblood said:
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
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top