I have installed ruby with “ruby181-11.exe” on Windows XP and wanted to try
some code samples from the Ruby Programmers Guide, but without success. If I
run following code, I don’t get any output on console:
···
a = [ 1, 'cat', 3.14 ] # array with three elements
# access the first element
a[0] » 1
# set the third element
a[2] = nil
# dump out the array
a » [1, "cat", nil]
on my machine (executing ‘ruby example.rb’) it outputs
1
[1, “cat”, nil]
···
On Fri, 05 Mar 2004 15:42:12 +0100, Yalin wrote:
I have installed ruby with “ruby181-11.exe” on Windows XP and wanted to try
some code samples from the Ruby Programmers Guide, but without success. If I
run following code, I don’t get any output on console:
–
Simon Strandgaard
I have installed ruby with “ruby181-11.exe” on Windows XP and
wanted to try
some code samples from the Ruby Programmers Guide, but without
success. If I
run following code, I don’t get any output on console:
a = [ 1, 'cat', 3.14 ] # array with three elements
# access the first element
a[0] » 1
# set the third element
a[2] = nil
# dump out the array
a » [1, "cat", nil]
Just the command “puts a” works.
Any idea?
The result of expressions are not automatically dumped to the console
(unless you are running irb), so you must use “puts” to dump values to the
console.
Curt
···
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.610 / Virus Database: 390 - Release Date: 3/3/2004
The problem is that “a[0] » 1” in the Ruby Programmers Guide doesn’t mean
that “a[0]” prints “1”, but merely that “1” is the result of the
expression “a[0]”. To actually see this result you’ll have to print it
with (for example) puts like this: “puts a[0]”.
Another way is to use “irb” which will print out the result of everything
you enter and therefore is a great enviromnent to try out these examples.
I have installed ruby with “ruby181-11.exe” on Windows XP and wanted to
try
some code samples from the Ruby Programmers Guide, but without success.
If I
run following code, I don’t get any output on console:
a = [ 1, 'cat', 3.14 ] # array with three elements
# access the first element
a[0] » 1
# set the third element
a[2] = nil
# dump out the array
a » [1, "cat", nil]
but what is the reason for this problem?
The same is, e.g. when you use the “inspect” or the “to_s” message of an
object.
···
–
Yalin
“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
news:pan.2004.03.05.15.05.13.627630@adslhome.dk…
On Fri, 05 Mar 2004 15:42:12 +0100, Yalin wrote:
I have installed ruby with “ruby181-11.exe” on Windows XP and wanted to
try
some code samples from the Ruby Programmers Guide, but without success.
If I
run following code, I don’t get any output on console:
[snip]
welcome to Ruby
try
a = [1, ‘cat’, 3.14]
p a[0]
a[2] = nil
p a
on my machine (executing ‘ruby example.rb’) it outputs
but what is the reason for this problem?
The same is, e.g. when you use the “inspect” or the “to_s” message of an
object.
p “hello” # “hello”
puts(“hello”.inspect) # “hello”
All objects has an .inspect method. Even though we
doesn’t define it ourselves then its there.
For instance a slightly more complex example…
then .inspect will do a nice output of the internal state
of the object.
class Person
def initialize(firstname, lastname) @firstname = firstname @lastname = lastname
end
end
person = Person.new(“Albert”, “Einstein”)
p person