Basic question about gets.chomp

Hi All,

Sorry if i am bothering you all with a silly question. I am new to
programming itself not just to ruby.

print "what is your age? "
age = gets.chomp()
puts “So you are %s years old.”, %age

This is not working as expected. It is erroring out “unterminated
string
meets end of file”

However if i use below line it works.

puts "So you are #{age} years old.

I am trying to understand the difference and reason behind this.

Any help is appreciated!

-Kaustubh

The command (yes consider it a command in this sense) puts does not
accept
the same formatting syntax as printf and sprintf. Does that help you?
Another variation and interface to consider with puts is:

puts "So you are " + age + “years old.”

~Stu

Kaustubh Chaudhari wrote in post #1135891:

Hi All,

Sorry if i am bothering you all with a silly question. I am new to
programming itself not just to ruby.

print "what is your age? "
age = gets.chomp()
puts “So you are %s years old.”, %age

This is not working as expected. It is erroring out “unterminated
string
meets end of file”

If we look at your last line, and remove the syntax sugar, we get one of
these:

puts(“So you are %s years old.”, %age)
puts(“So you are %s years old.”,).%(age)

The first one is invalid because %age by itself doesn’t make sense,
and the second one is bad because you have a trailing comma in the list
of parameters to puts.

I suspect what you meant was:

puts(“So you are %s years old.”.%(age))

or with some sugar:

puts “So you are %s years old.” % age

Note that % is a method on the String class.

Thanks Matthew and Stu for the reply.

I noticed my what you mentioned. ‘,’ with the puts statement was the
culprit.

Thanks for helping me to understand and learn!

Have a nice day ahead!

-Kaustubh

Matthew K. wrote in post #1135898:

Kaustubh Chaudhari wrote in post #1135891:

Hi All,

Sorry if i am bothering you all with a silly question. I am new to
programming itself not just to ruby.

print "what is your age? "
age = gets.chomp()
puts “So you are %s years old.”, %age

This is not working as expected. It is erroring out “unterminated
string
meets end of file”

If we look at your last line, and remove the syntax sugar, we get one of
these:

puts(“So you are %s years old.”, %age)
puts(“So you are %s years old.”,).%(age)

The first one is invalid because %age by itself doesn’t make sense,
and the second one is bad because you have a trailing comma in the list
of parameters to puts.

I suspect what you meant was:

puts(“So you are %s years old.”.%(age))

or with some sugar:

puts “So you are %s years old.” % age

Note that % is a method on the String class.