From: Thiago Massa Date: 2012-08-16T01:28:58+09:00 Subject: Re: Print method --0015175cab32e87b8804c7506be0 Content-Type: text/plain; charset=ISO-8859-1 Woops, typo. # 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 end end "abcd".reverse.print # I hope you enjoyed. On Wed, Aug 15, 2012 at 1:28 PM, 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): > > # 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/. >> >> > --0015175cab32e87b8804c7506be0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Woops, typo.

# Ruby has open classes so I can open = a already existing class, like of String, and declare methods to it
class String
=A0 def print
=A0 =A0 puts self
=A0 end
end

"abcd".reverse.print

<= /div>
# I hope you enjoyed.


--0015175cab32e87b8804c7506be0--