From: Mark Hubbart Date: 2004-04-03T10:18:51+09:00 Subject: Re: deciding between ruby and python On Apr 2, 2004, at 3:33 PM, Aredridel wrote: >> Calling to_s() seems kinda brutal from the Python POV. Perhaps this is >> one of the areas where Ruby chooses the follow the Perl school of >> Convenience. > > And it certainly is convenient: > > the following is hypothetical, but useful: Yes, this type of coding is very useful in Ruby... Like this example (simplified from something I wrote once...): class Markup initialize(style, text) @style, @text = style, text end def to_s "<#{@style}>#{@text}" end end Now put together an array of Markup instances, and join it to get a string. Nice, huh? This is especially useful if you want to dynamically determine what type of markup to generate: html = Markup.use_html{ elems.join "\n" } ansi = Markup.use_ansi{ elems.join "\n" } rtf = Markup.use_rtf { elems.join "\n" } > Useful? > > The tradition of duck-typing and various kinds of hammer to coerce > objects into acting like the appropriate types is an appealing part of > Ruby. I agree heartily. --Mark