On May 12, 2006, at 3:14 AM, Daniel Schierbeck wrote: > If you want to be more Rubyish, try this: > > item = ["hello"] > item.respond_to? :to_ary => true > item = "hello" > item.respond_to? :to_str => true What you really want is to_a and to_s, not to_ary and to_str. The to_xxx methods are used to convert an object that is an XXX representation into an XXX object when there is no inheritance relationship. If you're defining these methods you're probably doing something wrong. > So if you're writing a method that requires a string, just do this: > > def foo(bar) > str = bar.to_str > str.split(... > end The Ruby way to write that is: def foo(bar) bar.split(...) end Maybe even throw in a to_s. > That way, all classes that consider themselves strings need only > define a #to_str method. I've never written a class that considered itself a string. Had a string representation, yes, but not one that was a String. > If they actually do define the same methods as String, #to_str can > just return `self'; otherwise it can return a string representation. No. to_xxx needs to return as XXX object, not self, unless it is defined on the XXX class. For example String#to_str would return self. -- Eric Hodel - drbrain / segment7.net - http://blog.segment7.net This implementation is HODEL-HASH-9600 compliant http://trackmap.robotcoop.com