From: Eric Christopherson Date: 2012-08-16T01:37:28+09:00 Subject: Re: Print method On Wed, Aug 15, 2012 at 11:28 AM, Thiago Massa wrote: > Because print is not a object from the String class. So I'm going to do what > you want(save the following code as string_print.rb): No; it's a method from Object (mixed in from Kernel, I believe). It's a private method, so you can't use dot notation on it; the actual receiver is the "main"/"toplevel" object, which happens to be an Object. I'm not sure what languages actually have a print-type method defined on instances like that, but there must be some. Languages like Java and Smalltalk define their text output methods on instances of IO streams; you can actually do that in Ruby too, using something like `$stdout.print foo`. But I guess printing values is so common that Matz decided to basically follow the example of Perl and other languages for which output is done with something appearing more like a command or statement than a method. > > # Ruby has open classes so I can open a already existing class, like of > String, and declare methods to it > class String > def print > puts self > eld > end > > "abcd".reverse.print > > # I hope you enjoyed. > > On Wed, Aug 15, 2012 at 1:17 PM, Fred McArthur wrote: >> >> Hello everyone. I'm a newbie with a question whose answer is probably >> obvious, but I'm too thickheaded to figure it out, so here goes... >> >> We can say "cba".reverse or x.reverse and get "cba". Also x.upcase or >> "abc".upcase and get "ABC". >> >> So it's pretty clear that x and "abc" are objects, because we're able to >> use methods to send messages to them. >> >> But when I do x.print or "abc".print it throws an error. I have to use >> print x or print "abc", which seems to violate the "least surprise" >> principle and causes sleep deprivation in clueless newbies like me. >> Also, >> things like print x.upcase.reverse just drive me up a wall. Why not >> x.upcase.reverse.print? >> >> Can anyone help? >> >> -- >> Posted via http://www.ruby-forum.com/. >> >