noob question on combining variables

B

bingo bob

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

Andrew Timberlake

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

How do I combine =A0file_ and another variable NOT like this clearly...

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

but how?

If you're wanting the text "file_1" etc then
count =3D 1
"file_#{count}"

but then you're calling save on "file_1" which is a string

Andrew Timberlake
http://ramblingsonrails.com

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

bingo bob

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
 
B

bingo bob

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

resort."image_#[file_count]"

How do I do that?

Thanks
 
B

bingo bob

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
 
E

Etienne Vallette d'osia

bingo said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top