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 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?
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"
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"
Aha it worked!:
puts 'I think ' + (number.to_i + 1).to_s + ' may be a better favorite
number though...'
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...
-----------------------------------------------------
EOT
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"
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.
With the exception that this method returns an array, there is no
difference between it and print. Try:
print "foo ", "bar ", "baz"
···
On Tue, Nov 18, 2008 at 8:48 PM, Peña, Botp <botp@delmonte-phil.com> wrote:
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.