Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I "string".print()?

Tags:

python

ruby

My understanding of the print() in both Python and Ruby (and other languages) is that it is a method on a string (or other types). Because it is so commonly used the syntax:

print "hi"

works.

So why doesn't "hi".print() in Python or "hi".print in Ruby work?

like image 794
DorkRawk Avatar asked Sep 04 '12 23:09

DorkRawk


2 Answers

When you do something like "hi".print(), you are implying that the string object "hi" has a method print. This is not the case. Instead, print is a function that takes a string (or other types) as input.

like image 147
guyrt Avatar answered Oct 01 '22 18:10

guyrt


Ruby does have a method Object#display (doc here), which sends a representation of the object to the current output stream, or one specified as an argument.

(I find that it's hard to work with in irb if I use ; at the end of a line to suppress the printing of the return value; if I do that, display's output isn't shown, even if I flush the stream.)

like image 44
echristopherson Avatar answered Oct 01 '22 18:10

echristopherson