Noob question on combining variables

Sorry for such an uber noob question but I could do with a quick pointer
on this.

How do I combine file_ and another variable NOT like this clearly…

count = 1
file_"#{count}".save
count += 1
file_"#{count}".save
count += 1
file_"#{count}".save

but how?

On Thu, Aug 13, 2009 at 1:56 PM, bingo bob[email protected] wrote:

file_"#{count}".save

but how?

If you’re wanting the text “file_1” etc then
count = 1
“file_#{count}”

but then you’re calling save on “file_1” which is a string

Andrew T.
http://ramblingsonrails.com

http://MyMvelope.com - The SIMPLE way to manage your savings

here’s the real code I’m trying to fix…

         File.open(full_path_and_filename) { |photo_file| 

resort.“image_#[file_count]” = photo_file }
resort.save

?

bb

Any ideas, it’s this bit that’s wrong,

resort.“image_#[file_count]”

How do I do that?

Thanks

maybe

resort.send “image_#{file_count}”.to_sym

Best regards,
Sergey Avseyev

How does that work? Sorry not clear.

All I’m trying to do is get the count (which will be 1, 2 or 3) included
on the end of the “image_”

bb

On Thu, Aug 13, 2009 at 9:40 AM, Etienne Vallette
d’osia[email protected] wrote:

bingo bob wrote:

How does that work? Sorry not clear.

All I’m trying to do is get the count (which will be 1, 2 or 3) included
on the end of the “image_”

bb

resort.send(:“image_#{file_count}=”, photo_file)

It is not necessary to pass a symbol as a message, a string will do.

bingo bob wrote:

How does that work? Sorry not clear.

All I’m trying to do is get the count (which will be 1, 2 or 3) included
on the end of the “image_”

bb

resort.send(:“image_#{file_count}=”, photo_file)

  • you can’t build a variable name in ruby, if you want to do this, use
    hashes
  • :"" is a short form of “”.intern (or “”.to_sym)
  • a.b can be written a.send(:b)
  • when you write “a.b = 3”, you call the method “b=” of the object a
    with the argument 3.

Etienne

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs