Problems with Ruby under Windows XP

Y

Yalin

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]
 
S

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:
[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

1
[1, "cat", nil]
 
Y

Yalin

Thanks,

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 said:
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

1
[1, "cat", nil]
 
S

Simon Strandgaard

On Fri, 05 Mar 2004 16:18:50 +0100, Yalin wrote:
[snip]
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

#<Person:0x810a800 @firstname="Albert", @lastname="Einstein">
 
D

Dave Thomas

Thanks,

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.

If you want Ruby to output a value, you need to say something like

puts value

Simply saying

value

doesn't generate any output.

In the book, we add annotations to some code listing to show the values
of expressions on those lines. So we might have

value = 123
value + 2 # => 125

The reason we did that was that it saved a lot of space, comared with

value = 123
puts(value+2)

generates:

125


It's also easier to track what values ti in with which expressions.

Sorry it's confusing



Cheers

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top