# Syntax question from a newbie to Ruby

**URL:** <https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577>\
**Category:** Ruby\
**Created:** [November 18, 2008, 11:38pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577 "2008-11-18T23:38:15Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 18, 2008, 11:38pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/1 "2008-11-18T23:38:15Z")

</div>

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 ‘-----------------------------------------------------’

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 18, 2008, 11:48pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/2 "2008-11-18T23:48:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 18, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/3 "2008-11-18T23:50:46Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 19, 2008, 2:53am UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/4 "2008-11-19T02:53:16Z")

</div>

> 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…

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 18, 2008, 11:59pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/5 "2008-11-18T23:59:49Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 19, 2008, 8:35am UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/6 "2008-11-19T08:35:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 20, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/7 "2008-11-20T17:19:00Z")

</div>

Robert K. 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…”

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 19, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/8 "2008-11-19T19:55:08Z")

</div>

From: Mike A. [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”]]

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 20, 2008, 6:06pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/9 "2008-11-20T18:06:57Z")

</div>

On Tue, Nov 18, 2008 at 8:48 PM, Peña, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 21, 2008, 7:45am UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/10 "2008-11-21T07:45:34Z")

</div>

On 20.11.2008 17:15, Michael Tomer wrote:

> Robert K. 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

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [November 21, 2008, 3:17pm UTC](https://www.ruby-forum.com/t/syntax-question-from-a-newbie-to-ruby/153577/11 "2008-11-21T15:17:03Z")

</div>

> 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.
