You can also say
p “hello”
…which is what I do when I’m in a hurry.
You can also say
p “hello”
…which is what I do when I’m in a hurry.
You can also say
p “hello”
…which is what I do when I’m in a hurry.
This differs from #puts calls #to_s on its arguments while #p calls #inspect.
Gavin
From: “Stephen Neu” sneu@iblp.org
And print is also an ‘inspect’ type call? (ie interrogates the string to see
if there are escaped characters?)
–Andrew
On Tue, Nov 26, 2002 at 01:36:26PM +0900, Gavin Sinclair wrote:
From: “Stephen Neu” sneu@iblp.org
You can also say
p “hello”This differs from #puts calls #to_s on its arguments while #p calls #inspect.
From: “Stephen Neu” sneu@iblp.org
You can also say
p “hello”This differs from #puts calls #to_s on its arguments while #p calls
#inspect.And print is also an ‘inspect’ type call? (ie interrogates the string to see
if there are escaped characters?)
According to ‘ri’, #print calls #to_s on non-String arguments.
Gavin
From: “andrew delboy” andrew@cyber.com.au
On Tue, Nov 26, 2002 at 01:36:26PM +0900, Gavin Sinclair wrote:
Ok, let me see if I have gotten my head around this.
After a bit of mailing list searching, this is what I have
come up with.
Method Names | How they work
Does this about sum up the differences??
–Andrew
Method Names | How they work
#print | using the #to_s on each item within
> the presented ‘string’, and appends a
> ‘space’ character after the concatonated
> strings (and the space can get redirected
> to another character) - irb seemed to not
> put spaces, but I havent tested it with
> the actual ruby interpreter…
I did test it with the interpreter. print does not put spaces by
default. This is the behaviour I would expect.
#puts | using the #to_s on each item within
> the presented ‘string’, and appents a new
> line character after the concatonated
> strings (perhaps the newline character
> can be redirected to another character
> as well??)
It joins the strings via newlines.
$ ruby -e ‘puts “hello”, “world”’
hello
world
#to_s | calls the ‘stringify method’ which prints
> a pretty version of the string (such as
> a way to print arrays in a meaningful way
> to a human)
This one is cool.
class Foo
def initialize
@var = “bar”
end
def to_s
“This is a customized string”
end
end
var = Foo.new
puts var
Prints out:
“This is a customized string”
I only expect the “space-adding” behavior in Python. Most other languages
that I’ve used won’t insert spaces where you don’t ask for them with a
“print”. Then again, I haven’t used that many languages, so maybe I don’t
know what the expected behavior should be
-Brian W
Brian Wisti
brian@coolnamehere.com
http://coolnamehere.com/
At 02:54 PM 11/26/2002 +0900, you wrote:
Method Names | How they work
#print | using the #to_s on each item within
> the presented ‘string’, and appends a
> ‘space’ character after the concatonated
> strings (and the space can get redirected
> to another character) - irb seemed to not
> put spaces, but I havent tested it with
> the actual ruby interpreter…I did test it with the interpreter. print does not put spaces by
default. This is the behaviour I would expect.
Whereas if you try the following:
it prints:
#<Foo:0x401f36b4 @var=“bar”>
Is there a way to override this behavior? So you could put
something different much like the array or hash printout with
the #p method?
–Andrew
#to_s | calls the ‘stringify method’ which prints
> a pretty version of the string (such as
> a way to print arrays in a meaningful way
> to a human)This one is cool.
class Foo
def initialize
@var = “bar”
end
def to_s
“This is a customized string”
end
endvar = Foo.new
puts var
Prints out:
“This is a customized string”
Whereas if you try the following:
p var
it prints:
#<Foo:0x401f36b4 @var=“bar”>Is there a way to override this behavior? So you could put
something different much like the array or hash printout with
the #p method?
class Foo
def inspect
“Just override ‘inspect’”
end
end
p var # → “Just override ‘inspect’”
Notice that I didn’t have to do a ‘var Foo.new’ again. var was already a
Foo, so when I modified Foo, var was changed correspondigly.
–Andrew
#to_s | calls the ‘stringify method’ which prints
> a pretty version of the string (such as
> a way to print arrays in a meaningful way
> to a human)This one is cool.
class Foo
def initialize
@var = “bar”
end
def to_s
“This is a customized string”
end
endvar = Foo.new
puts var
Prints out:
“This is a customized string”
Even better:
class Foo
def inspect
self.to_s
end
end
~Daniel
Whereas if you try the following:
p var
it prints:
#<Foo:0x401f36b4 @var=“bar”>Is there a way to override this behavior? So you could put
something different much like the array or hash printout with
the #p method?–Andrew
#to_s | calls the ‘stringify method’ which prints
> a pretty version of the string (such as
> a way to print arrays in a meaningful way
> to a human)This one is cool.
class Foo
def initialize
@var = “bar”
end
def to_s
“This is a customized string”
end
endvar = Foo.new
puts var
Prints out:
“This is a customized string”