From: Brian Candler Date: 2009-05-27T18:24:09+09:00 Subject: Re: String representing relative file name, adding (meta) data? Markus Fischer wrote: > The downsize is, of course, it's not a string. So whenever I need a > string operation, I need to explicitly call .to_s, but I can live with > that. Would be nice if it could automatically work in string context, > like concatentation, too. Some Ruby methods call to_str implicitly when they expect a String, so try that. irb(main):005:0> class Foo; def initialize(s) @s=s; end; def to_str; @s; end; end => nil irb(main):006:0> str1 = "foo" => "foo" irb(main):007:0> str2 = Foo.new("bar") => # irb(main):008:0> str1 << str2 => "foobar" Also, you may wish to delegate other string-like methods to @name inside class Resourcename, e.g. def <<(other) @name.<<(other) end def [](*args) @name[*args] end .. etc This lets you pick and choose the ones which you wish to expose. Otherwise, you can delegate all unknown methods like this: def method_missing(m, *args, &blk) @name.send(m, *args, &blk) end -- Posted via http://www.ruby-forum.com/.