Variable in string in array

Anybody know how to embed a #{} in a string and then place the string in
an array?

eg.

array =[“An #{age} year old…”]

returns
/Applications/TextMate.app/Contents/SharedSupport/Bundles/Ruby.tmbundle/Support/RubyMate/catch_exception.rb:15

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

Simply array =[‘An #{age} year old…’]

Should work, no?

Paul B.
Technical Consultant

Anthony Hodges Consulting Limited
Highly Commended - European Pensions Communications Awards 2012
Highly Commended - Communications Specialist (UK Pension and Investment
Provider Awards 2012)
Winner - Communications Provider of the Year (UK Pensions Awards 2011)
Winner - Communications Specialist (UK Pension and Investment Provider
Awards 2011)
Highly Commended - European Pensions Communications Awards 2011

Direct line: +44 (0)1924 203931
Switchboard: +44 (0)1924 203900
Email: [email protected]
Website: http://www.ahc.uk.com

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

2012/8/15 Paul B. [email protected]:

Simply array =[‘An #{age} year old…’]

Should work, no?

Not with single quotes.

– Matma R.

On 15.08.2012 13:46, Dave C. wrote:

/Applications/TextMate.app/Contents/SharedSupport/Bundles/Ruby.tmbundle/Support/RubyMate/catch_exception.rb:15

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:

age = 99
=> 99

array =[“A #{age} year old…”]
=> [“A 99 year old…”]

On Wed, Aug 15, 2012 at 2:46 PM, Dave C. [email protected]
wrote:

Anybody know how to embed a #{} in a string and then place the string in
an array?

eg.

array =[“An #{age} year old…”]

returns

/Applications/TextMate.app/Contents/SharedSupport/Bundles/Ruby.tmbundle/Support/RubyMate/catch_exception.rb:15

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

Robert K. wrote in post #1072441:

On Wed, Aug 15, 2012 at 2:46 PM, Dave C. [email protected]
wrote:

Anybody know how to embed a #{} in a string and then place the string in
an array?

eg.

array =[“An #{age} year old…”]

returns

/Applications/TextMate.app/Contents/SharedSupport/Bundles/Ruby.tmbundle/Support/RubyMate/catch_exception.rb:15

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]

An 11 year old…

Posted by Dave C. (dcastellano1) on 2012-08-15 18:07
Robert K. wrote in post #1072441:

On Wed, Aug 15, 2012 at 2:46 PM, Dave C. [email protected]
wrote:

Anybody know how to embed a #{} in a string and then place the string in
an array?

eg.

array =[“An #{age} year old…”]

returns

/Applications/TextMate.app/Contents/SharedSupport/Bundles/Ruby.tmbundle/Support/RubyMate/catch_exception.rb:15

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]

An 11 year old…

Please trim your quotes. Thank you!

On Wed, Aug 15, 2012 at 6:07 PM, Dave C. [email protected]
wrote:

I want to place it in the array as a string, evaluate it later…
eg:

age = “11”
puts array[0]

The simplest would be

array = [ lambda {|age| “An #{age} year old…”} ]

Then you can do

puts array[0][11]

or

all_strings = array.map {|f| f[11]}

or

single_string = values = array.map {|f| f[11]}.join

Kind regards

robert

On Fri, Aug 17, 2012 at 1:06 AM, Dave C. [email protected]
wrote:

array = [ lambda { |age| “An #{age} year old…”}, 20, 50 ]

puts array[0][age returned from number_range method]

Yes, exactly like that.

Kind regards

robert

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

array = [ lambda { |age| “An #{age} year old…”}, 20, 50 ]

puts array[0][age returned from number_range method]

An 18 year old…