Forum: Ruby Syntax question from a newbie to Ruby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ed3ab542a08789f18bc971ce7bb7fd19?d=identicon&s=25 David Spitzer (davidspitzer)
on 2008-11-18 23:38
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 '-----------------------------------------------------'
Ed3ab542a08789f18bc971ce7bb7fd19?d=identicon&s=25 David Spitzer (davidspitzer)
on 2008-11-18 23:48
David Spitzer wrote:
> 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?
4fea1ef11180adaaa299d503ca6010d0?d=identicon&s=25 John W Kennedy (Guest)
on 2008-11-18 23:50
(Received via mailing list)
David Spitzer wrote:
> 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"
Ed3ab542a08789f18bc971ce7bb7fd19?d=identicon&s=25 David Spitzer (davidspitzer)
on 2008-11-18 23:59
John W Kennedy wrote:
> David Spitzer wrote:
>> 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"

Aha it worked!:

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

thanks!
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2008-11-19 02:53
(Received via mailing list)
> 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...
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-19 08:35
(Received via mailing list)
On 18.11.2008 23:56, David Spitzer wrote:

> 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
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-19 19:55
(Received via mailing list)
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
=> nil

> prints "I think ", number + 1, " may be a better favorite\n"   I think 2 may be a better 
favorite
=> ["I think ", 2, " may be a better favorite\n"]


w the added adv that you get an array (of listing) too, ergo, the ff
works, too

> prints (prints "I think ", number + 1, " may be a better favorite\n")
I think 2 may be a better favorite
I think 2 may be a better favorite
=> [["I think ", 2, " may be a better favorite\n"]]
6d9fdb4fe716a92211de779a7e500821?d=identicon&s=25 Michael Tomer (michael_tomer)
on 2008-11-20 17:19
Robert Klemme wrote:
> 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..."
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2008-11-20 18:06
(Received via mailing list)
On Tue, Nov 18, 2008 at 8:48 PM, Peña, Botp <botp@delmonte-phil.com> wrote:
>>   end
>> end
> => nil

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

print "foo ", "bar ", "baz"
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-11-21 07:45
(Received via mailing list)
On 20.11.2008 17:15, Michael Tomer wrote:
> Robert Klemme wrote:
>> 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 #{}.

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
6d9fdb4fe716a92211de779a7e500821?d=identicon&s=25 Michael Tomer (michael_tomer)
on 2008-11-21 15:17
>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.
This topic is locked and can not be replied to.