Syntax question from a newbie to Ruby

D

David Spitzer

I am just learning Ruby and I can not seem to see why the first example
works but the second one does not - thanks

Why does this work:

puts 'Hello what is you\'re Favorite Number?'
number = gets.chomp
puts''
puts'-----------------------------------------------------'
puts 'Your favorite number is ' ''+ number + '?'
puts ''
puts 'What a Lovely Number!'
puts ''
puts 'I think '
puts (number.to_i + 1)
puts 'may be a better favorite number though...'
puts '-----------------------------------------------------'



But this does not:

puts 'Hello what is you\'re Favorite Number?'
number = gets.chomp
puts''
puts'-----------------------------------------------------'
puts 'Your favorite number is ' ''+ number + '?'
puts ''
puts 'What a Lovely Number!'
puts ''
puts 'I think ' + (number.to_i + 1) + ' may be a better favorite number
though...'
puts '-----------------------------------------------------'
 
D

David Spitzer

David said:
I am just learning Ruby and I can not seem to see why the first example
works but the second one does not - thanks

Why does this work:

puts 'Hello what is you\'re Favorite Number?'
number = gets.chomp
puts''
puts'-----------------------------------------------------'
puts 'Your favorite number is ' ''+ number + '?'
puts ''
puts 'What a Lovely Number!'
puts ''
puts 'I think '
puts (number.to_i + 1)
puts 'may be a better favorite number though...'
puts '-----------------------------------------------------'



But this does not:

puts 'Hello what is you\'re Favorite Number?'
number = gets.chomp
puts''
puts'-----------------------------------------------------'
puts 'Your favorite number is ' ''+ number + '?'
puts ''
puts 'What a Lovely Number!'
puts ''
puts 'I think ' + (number.to_i + 1) + ' may be a better favorite number
though...'
puts '-----------------------------------------------------'

i got it to work with this:

puts 'Hello what is you\'re Favorite Number?'
number = gets.chomp
puts''
puts'-----------------------------------------------------'
puts 'Your favorite number is ' ''+ number + '?'
puts ''
puts 'What a Lovely Number!'
puts ''
newnumber = (number.to_i + 1.to_i)
puts 'I think ' + newnumber.to_s + ' may be a better favorite number
though...'
puts '-----------------------------------------------------'

is there a way to get it to work with another variable (newnumber)
adding them first then converting it to a string in the text line?
 
J

John W Kennedy

David said:
puts 'Hello what is you\'re Favorite Number?'

Well, there's your /first/ syntax error.
puts 'I think ' + (number.to_i + 1) + ' may be a better favorite number
though...'

Since "+" can mean either "concatenate strings" or "add numbers", you
need a "to_s" there, just as you needed "to_i" before trying arithmetic.
--
John W. Kennedy
"Though a Rothschild you may be
In your own capacity,
As a Company you've come to utter sorrow--
But the Liquidators say,
'Never mind--you needn't pay,'
So you start another company to-morrow!"
-- Sir William S. Gilbert. "Utopia Limited"
 
D

David Spitzer

John said:
Well, there's your /first/ syntax error.


Since "+" can mean either "concatenate strings" or "add numbers", you
need a "to_s" there, just as you needed "to_i" before trying arithmetic.
--
John W. Kennedy
"Though a Rothschild you may be
In your own capacity,
As a Company you've come to utter sorrow--
But the Liquidators say,
'Never mind--you needn't pay,'
So you start another company to-morrow!"
-- Sir William S. Gilbert. "Utopia Limited"

Aha it worked!:

puts 'I think ' + (number.to_i + 1).to_s + ' may be a better favorite
number though...'

thanks!
 
M

Mike Austin

David said:
Aha it worked!:

puts 'I think ' + (number.to_i + 1).to_s + ' may be a better favorite
number though...'

thanks!

I don't see use of Ruby's streams used much in examples:

$stdout << "I think " << number + 1 << " may be a better favorite\n"

It's much cleaner, although not as simple a concept as print.

Mike
 
P

Peña, Botp

RnJvbTogTWlrZSBBdXN0aW4gW21haWx0bzoibWlrZVtub3NwYW1dIkBtaWtlLWF1c3Rpbi5jb21d
IA0KLi4NCiMgJHN0ZG91dCA8PCAiSSB0aGluayAiIDw8IG51bWJlciArIDEgPDwgIiBtYXkgYmUg
YSBiZXR0ZXIgZmF2b3JpdGVcbiINCiMgSXQncyBtdWNoIGNsZWFuZXIsIGFsdGhvdWdoIG5vdCBh
cyBzaW1wbGUgYSBjb25jZXB0IGFzIHByaW50Lg0KDQp3ZWxsIHlvdSBjYW4gZXh0ZW5kIHJ1Ynkg
dG8geW91ciBsaWtpbmcsDQoNCj4gZGVmIHByaW50cyAqbGlzdA0KPiAgIGxpc3QuZWFjaCBkbyB8
aXRlbXwNCiogICAgICRzdGRvdXQgPDwgaXRlbQ0KPiAgIGVuZA0KPiBlbmQNCj0+IG5pbA0KDQo+
IHByaW50cyAiSSB0aGluayAiLCBudW1iZXIgKyAxLCAiIG1heSBiZSBhIGJldHRlciBmYXZvcml0
ZVxuIiAgIEkgdGhpbmsgMiBtYXkgYmUgYSBiZXR0ZXIgZmF2b3JpdGUNCj0+IFsiSSB0aGluayAi
LCAyLCAiIG1heSBiZSBhIGJldHRlciBmYXZvcml0ZVxuIl0NCg0KDQp3IHRoZSBhZGRlZCBhZHYg
dGhhdCB5b3UgZ2V0IGFuIGFycmF5IChvZiBsaXN0aW5nKSB0b28sIGVyZ28sIHRoZSBmZiB3b3Jr
cywgdG9vDQoNCj4gcHJpbnRzIChwcmludHMgIkkgdGhpbmsgIiwgbnVtYmVyICsgMSwgIiBtYXkg
YmUgYSBiZXR0ZXIgZmF2b3JpdGVcbiIpDQpJIHRoaW5rIDIgbWF5IGJlIGEgYmV0dGVyIGZhdm9y
aXRlDQpJIHRoaW5rIDIgbWF5IGJlIGEgYmV0dGVyIGZhdm9yaXRlDQo9PiBbWyJJIHRoaW5rICIs
IDIsICIgbWF5IGJlIGEgYmV0dGVyIGZhdm9yaXRlXG4iXV0NCg0K
 
M

Matthew Moss

i got it to work with this:
puts 'Hello what is you\'re Favorite Number?'
number = gets.chomp
puts''
puts'-----------------------------------------------------'
puts 'Your favorite number is ' ''+ number + '?'
puts ''
puts 'What a Lovely Number!'
puts ''
newnumber = (number.to_i + 1.to_i)
puts 'I think ' + newnumber.to_s + ' may be a better favorite number
though...'
puts '-----------------------------------------------------'

is there a way to get it to work with another variable (newnumber)
adding them first then converting it to a string in the text line?


Also, try this:

puts <<EOT
-----------------------------------------------------
Your favorite number is #{number}?

What a Lovely Number!

I think #{number.to_i + 1} may be a better favorite number though...
 
R

Robert Klemme

Aha it worked!:

puts 'I think ' + (number.to_i + 1).to_s + ' may be a better favorite
number though...'

I'd rather do

puts "I think #{number.to_i + 1} may be a better favorite number though..."

Or, if you are a fan of printf

printf "I think %d may be a better favorite number though...\n",
number.to_i + 1

Kind regards

robert
 
M

Michael Tomer

Robert said:
On 18.11.2008 23:56, David Spitzer wrote:
puts "I think #{number.to_i + 1} may be a better favorite number
though..."

I think you can skip the .to_i when the number is inside #{}. In other
words, I think it would be valid and a little more readable to say:

puts "I think #{number + 1} may be a better favorite number though..."
 
G

Gregory Brown

From: Mike Austin [mailto:"mike[nospam]"@mike-austin.com]
..
# $stdout << "I think " << number + 1 << " may be a better favorite\n"
# It's much cleaner, although not as simple a concept as print.

well you can extend ruby to your liking,
def prints *list
list.each do |item| * $stdout << item
end
end
=3D> nil

With the exception that this method returns an array, there is no
difference between it and print. Try:

print "foo ", "bar ", "baz"




--=20
Technical Blaag at: http://blog.majesticseacreature.com | Non-tech
stuff at: http://metametta.blogspot.com
 
R

Robert Klemme

I think you can skip the .to_i when the number is inside #{}.

No. Whether you can skip to_i solely depends on the type of "number".
Btw, you can easily test that (see below).
In other
words, I think it would be valid and a little more readable to say:

puts "I think #{number + 1} may be a better favorite number though..."

irb(main):001:0> "1"+2
TypeError: can't convert Fixnum into String
from (irb):1:in `+'
from (irb):1
irb(main):002:0>

This has nothing to do where the expression appears. IIRC number is a
String here so you must convert it to do integer math.

Cheers

robert
 
M

Michael Tomer

IIRC number is a String here so you must convert it to do integer math.
Yes, you're completely right. I forgot he grabbed the variable from
STDIN.
 

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

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top