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

···

--
Posted via http://www.ruby-forum.com/.

Simply array =['An #{age} year old...']

Should work, no?

Paul Brennan
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: paul.brennan@ahc.uk.com
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

···

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

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

--
Posted via http://www.ruby-forum.com/.

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

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 15.08.2012 13:46, Dave Castellano wrote:

--
Alex Gutteridge

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

···

On Wed, Aug 15, 2012 at 2:46 PM, Dave Castellano <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

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]

···

On Wed, Aug 15, 2012 at 2:46 PM, Dave Castellano <lists@ruby-forum.com> > wrote:

An 11 year old...

--
Posted via http://www.ruby-forum.com/\.

Not with single quotes.

-- Matma Rex

···

2012/8/15 Paul Brennan <paul.brennan@ahc.uk.com>:

Simply array =['An #{age} year old...']

Should work, no?

Robert Klemme wrote in post #1072441:

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:

age = "11"
puts array[0]

···

On Wed, Aug 15, 2012 at 2:46 PM, Dave Castellano <lists@ruby-forum.com> > wrote:

An 11 year old...

--
Posted via http://www.ruby-forum.com/\.

Please trim your quotes. Thank you!

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 Wed, Aug 15, 2012 at 6:07 PM, Dave Castellano <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

--
Posted via http://www.ruby-forum.com/\.

Yes, exactly like that.

Kind regards

robert

···

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

array = [ lambda { |age| "An #{age} year old..."}, 20, 50 ]

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/