From: Thiago Massa Date: 2012-08-16T01:28:08+09:00 Subject: Re: Print method --0015175cae665f94f004c7506979 Content-Type: text/plain; charset=ISO-8859-1 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/. > > --0015175cae665f94f004c7506979 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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):

<= div># 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 eld
end

"abcd".reverse.print
=
# I hope you enjoyed.


--0015175cae665f94f004c7506979--