So I am new to ruby, and I seem to be stuck on a seemingly simple
problem. I
have two variable’s, one that is defined in the script, and one that is
grabbed from input. I can add them together just fine, but I have not
come
up with a way to put the addition statement inline with strings. For
instance:
puts favnum.to_i + plus1.to_i
Works producing the expected result. However when I try something like
this:
puts 'Try ' + favnum + plus1 + ' has the result.'
I get errors. I have tried declaring the variables as integers, and
tried
various syntax’s on that line, but the solution is escaping me and I
have
not found a good way to do this. I would also be willing to do a
variable to
define the item I want to add into the line, such as:
finalnum = favnum.to_i + plus1.to_i
But that doesnt produce the desired result either as the following
doesnt
work:
puts 'Try '+ finalnum +' instead.'
In both cases I also tried forcing .to_i but that didnt help. (though
doing
a “puts finalnum” works.)
So what sort of silly syntactical error am I making ?
(Apologies if this is the wrong place to post this, it seemed like my
best
bet.)
~Clockwork
On Aug 3, 2007, at 11:48 AM, [email protected] wrote:
variable to
In both cases I also tried forcing .to_i but that didnt help.
(though doing
a “puts finalnum” works.)
So what sort of silly syntactical error am I making ?
(Apologies if this is the wrong place to post this, it seemed like
my best
bet.)
~Clockwork
Very simple actually,
read those error messages!
You’re trying to do math and concatenation of strings at the same time.
so you have some options.
One of them is using .to_s on those numbers to turn them into strings.
But if you wan the result of adding or subtracting or what have you,
then you need parentheses around the math part, followed by .to_s
puts ‘Try ’ + (favnum + plus1).to_s + ’ has the result.’
It seems like you are coming from Perl or PHP where such expressions are
converted for you. This is not the case in Ruby. The language may be
dynamicly typed, but it’s strongly typed, so adding a FixNum to a String
doesn’t make sense. Thus, you’re best off doing the following:
favnum = favnum.to_i
plus1 = plus1.to_i
and continue with actual FixNum objects. Also, make libral use of string
replacements:
puts “Try #{favnum + plus1} …”
Such a thing calls #to_s on the result of whats in #{…}.
Welcome to Ruby!
Jason
On 8/3/07, [email protected] [email protected] wrote:
So what sort of silly syntactical error am I making ?
Ruby does not implicitly convert Integers into Strings. You need to
explicitly tell Ruby this is what you are doing.
puts “Try #{finalnum} instead”
Inside double quoted strings, anything inside a #{ } delimiter is
treated as Ruby code, and it is evaluated by the interpreter and
converted into a string. So you could also write (as you did
originally)
puts “Try #{favnum + plus1} has the result.”
Hope that makes sense. Oh, and welcome to Ruby!
Blessings,
TwP
When you are defining the one variable in the script are you defining it
as
a string? i.e. var = “3”
On 8/3/07, [email protected] [email protected] wrote:
Works producing the expected result. However when I try something like
finalnum = favnum.to_i + plus1.to_i
So what sort of silly syntactical error am I making ?
(Apologies if this is the wrong place to post this, it seemed like my best
bet.)
~Clockwork
–
“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”
-Greg Graffin (Bad Religion)
Awesome. Thanks.
Whats the difference between single and double quoted strings in ruby ?
On 8/3/07, [email protected] [email protected] wrote:
Awesome. Thanks.
Whats the difference between single and double quoted strings in ruby ?
Take a look at this section on Strings from the Programming Ruby book
…
http://www.rubycentral.com/pickaxe/language.html#UD
That will answer your question in much greater detail than I could in
this short mailing list post.
Blessings,
TwP