From: sto.mar@... Date: 2012-08-17T17:50:29+09:00 Subject: Re: Print method Am 17.08.2012 06:23, schrieb Fred McArthur: > So initially I said that Thiago Massa's class String extension worked, > and it does to a point. But not for numbers, at least not without > further mods. > > class String > def print > puts self > end > end > > "abcd".print # This works > > 1.print # This causes a Fixnum error Of course, since there is no print method in the Fixnum class. You would have to monkey patch that, too. > because "print/puts expects a string,and it will call to_s automatically > on the argument if it is not." Only the print method from the kernel will do that, but 1.print tries to invoke the print method from Fixnum. You would have to define a print method for *all* the classes that you want to work with. > What's nice about this is that it's automatic. No need to convert a non > display number to display type in order to print it. Saves a lot of > work. Please keep in mind that too much monkey patching might be counterproductive or even dangerous. In your case it does *not* save you any work, rather the contrary. And locating errors might get more difficult. I would *highly* recommend to simply stick with: print 'abc' print 1 puts 'result: ' << (1+2).to_s puts "result: #{1+2}" (There is no need to reinvent the language.) --