I tried adding single quotes
array =[' "An #{age} year old..." '] and this accepted the string but
the variable was also converted to a string, so lost as a variable.
Making a long term difference to the lives people lead
Registered in England & Wales, registered no. 3559492
Registered Office: Heath Hall, Heath, Wakefield, WF1 5SL
···
-----Original Message-----
From: Dave Castellano [mailto:lists@ruby-forum.com]
Sent: 15 August 2012 13:46
To: ruby-talk ML
Subject: Variable in string in array
Anybody know how to embed a #{} in a string and then place the string in an array?
I tried adding single quotes
array =[' "An #{age} year old..." '] and this accepted the string but
the variable was also converted to a string, so lost as a variable.
I tried adding single quotes
array =[' "An #{age} year old..." '] and this accepted the string but
the variable was also converted to a string, so lost as a variable.
Thanks
The code snippet you post is correct, so whatever exception TextMate is catching has been thrown from somewhere else:
If the error originated in that line it can only be caused by invoking
method "age". Do you have a stack trace?
I tried adding single quotes
array =[' "An #{age} year old..." '] and this accepted the string but
the variable was also converted to a string, so lost as a variable.
Do you want the #{...} to be literally present in the String or do you
want to evaluate it when placing the String in the Array?
Kind regards
robert
···
On Wed, Aug 15, 2012 at 2:46 PM, Dave Castellano <lists@ruby-forum.com> wrote:
If the error originated in that line it can only be caused by invoking
method "age". Do you have a stack trace?
I tried adding single quotes
array =[' "An #{age} year old..." '] and this accepted the string but
the variable was also converted to a string, so lost as a variable.
Do you want the #{...} to be literally present in the String or do you
want to evaluate it when placing the String in the Array?
Kind regards
robert
I want to place it in the array as a string, evaluate it later...
eg:
array =[' "An #{age} year old..." ']
age = "11"
puts array[0]
···
On Wed, Aug 15, 2012 at 2:46 PM, Dave Castellano <lists@ruby-forum.com> > wrote:
If the error originated in that line it can only be caused by invoking
method "age". Do you have a stack trace?
I tried adding single quotes
array =[' "An #{age} year old..." '] and this accepted the string but
the variable was also converted to a string, so lost as a variable.
Do you want the #{...} to be literally present in the String or do you
want to evaluate it when placing the String in the Array?
Kind regards
robert
I want to place it in the array as a string, evaluate it later...
eg:
age = "11"
puts array[0]
···
On Wed, Aug 15, 2012 at 2:46 PM, Dave Castellano <lists@ruby-forum.com> > wrote:
On Fri, Aug 17, 2012 at 1:06 AM, Dave Castellano <lists@ruby-forum.com> wrote:
Is it possible to pass elements in an array (eg 20,50 ) to the function
number_range(min,max) and have the returned value used in the variable
>age> below...
def number_range(min,max) # Chooses a random number within
specified range.
num = min + rand(max - min)
end