How to put multiple values into a variable


#1

I am new to coding, I have tried to place a number of data strings into
one variable but I am missing something in the format. See example
below.
Note each part of the line after = does have a real value, I just
can’t format the string.

file-link = Dir.pwd, “#{filename}”, “#{mp3-title}”, “#{mp3-artist}”


#2

On Sat, Feb 28, 2009 at 9:37 AM, Harry N. removed_email_address@domain.invalid
wrote:

I am new to coding, I have tried to place a number of data strings into
one variable but I am missing something in the format. See example
below.
Note each part of the line after = does have a real value, I just
can’t format the string.

file-link = Dir.pwd, “#{filename}”, “#{mp3-title}”, “#{mp3-artist}”

You did put all the strings into an array called file-link (that is,
unless you are getting a specific error that relates to your strings).
You could be trying to print or rely upon something that doesn’t
exist. Do you get NilClass errors?

For the unwary, try this to see how multiple assignment works (1.8.6)…

a, b = 1, 2

a => 1, b => 2

a, b = 1, 2, 3

a => 1, b => 2 ---- 3 is just lost

a = 1, 2

a => [1, 2]

a, *b = 1, 2, 3

a => 1, b => [2, 3]

hth,
Todd


#3

Harry N. wrote:

I am new to coding, I have tried to place a number of data strings into
one variable but I am missing something in the format. See example
below.
Note each part of the line after = does have a real value, I just
can’t format the string.

file-link = Dir.pwd, “#{filename}”, “#{mp3-title}”, “#{mp3-artist}”

You can use + to concatenate strings:

file_link = Dir.pwd + filename + mp3_title + mp3_artist

(Ruby doesn’t allow “-” characters in variable names so I changed them
to underscores.)

If you want blanks between each part, try this:

file_link = Dir.pwd + " " + filename + " " + mp3_title + " " +
mp3_artist

or this:

file_link = “#{Dir.pwd} #{filename} #{mp3_title} #{mp3_artist}”


#4

Harry N. wrote:

I am new to coding, I have tried to place a number of data strings into
one variable but I am missing something in the format. See example
below.
Note each part of the line after = does have a real value, I just
can’t format the string.

file-link = Dir.pwd, “#{filename}”, “#{mp3-title}”, “#{mp3-artist}”

TTBOMK, Ruby variable names cannot include hyphens.


#5

On Sat, Feb 28, 2009 at 9:54 AM, Jeff S. removed_email_address@domain.invalid
wrote:

TTBOMK, Ruby variable names cannot include hyphens.
Good eye. I missed that one. Also to the OP, you could do what you
did in your code and use #join instead of a bunch of + or <<
operators.

Todd


#6

Harry N. removed_email_address@domain.invalid writes:

I am new to coding, I have tried to place a number of data strings into
one variable

This is not possible. By definition a variable has only one value.
This is not quantic computing.

What you could do is to put the values in some object, and put that
object in the variable. You may choose between a custom object, a
hash-table, an array, a list, etc. For example:

filename = “/tmp/example.mp3”
mp3_title = “Example”
mp3_artist = “Musician”

file_link = [ Dir.pwd, filename, mp3_title, mp3_artist ]
file_link[0]
file_link[1]

file_link = { :directory => Dir.pwd,
:file => filename,
:title => mp3_title,
:artist => mp3_artist }
file_link[:directory]
file_link[:file]

class Cons
attr_accessor :first,:rest

def initialize(f,r)
@first=f
@rest=r
end

def Cons.list(*objects)
result = nil
objects . reverse . each { | object | result =
Cons.new(object,result) }
result
end

end

file_link = Cons.list( Dir.pwd, filename, mp3_title, mp3_artist )
file_link . first
file_link . rest . first

but I am missing something in the format. See example
below.

What format? Are you speaking of the syntax of the language, or do
you want to format a string? I ask because the expression you show us
is not even syntactically valid…

Note each part of the line after = does have a real value,
I just can’t format the string.

file-link = Dir.pwd, “#{filename}”, “#{mp3-title}”, “#{mp3-artist}”

So, assuming you want to build a new string formated with these
elements, separated by commas, you can do that with sprintf:

file_link = sprintf("%s, %s, %s, %s", Dir.pwd, filename, mp3_title,
mp3_artist )

If you have several lines to format, you may want to specify column
widths:

file_link = sprintf("%-20s, %-20s, %-30s, %s", Dir.pwd, filename,
mp3_title, mp3_artist )

Also, Ruby is rather more limited than Lisp, you cannot put a dash
inside an identifier.


#7

file-link = Dir.pwd, “#{filename}”, “#{mp3-title}”, “#{mp3-artist}”

class FileLink
def initialize(pwd, filename, title, artist)
@pwd = pwd
@filename = filename
@title = title
@artist = artist
end

attr_accessor :pwd, :filename, :title, :artist
end

a = FileLink.new(Dir.pwd, “Misora Hibari - Midaregami”, “Midaregami”,
“Misora Hibari”)
a.title

Macintosh:Yosh sai$ irb

class FileLink
def initialize(pwd, filename, title, artist)
@pwd = pwd
@filename = filename
@title = title
@artist = artist
end
attr_accessor :pwd, :filename, :title, :artist

?> end
=> nil

?> a = FileLink.new(Dir.pwd, “Misora Hibari - Midaregami”, “Midaregami”,
“Misora Hibari”)
=> #<FileLink:0x5e0a9c @title=“Midaregami”, @filename=“Misora Hibari -
Midaregami”, @pwd="/Users/sai/Documents/Projets/Yosh", @artist=“Misora
Hibari”>

a.title
=> “Midaregami”


#8

file_link = Dir.pwd + " " + filename + " " + mp3_title + " " + mp3_artist

or this:

file_link = “#{Dir.pwd} #{filename} #{mp3_title} #{mp3_artist}”

I vaguely remember a blog article with a comparison of several ways of
concatenating strings. This second form was the fastest. If I’m not
mistaken, the above version would create about 9 string objects (not
counting the variables).


#9

On Sat, Feb 28, 2009 at 9:07 PM, Harry N. removed_email_address@domain.invalid
wrote:

I am new to coding, I have tried to place a number of data strings into
one variable but I am missing something in the format. See example
below.
Note each part of the line after = does have a real value, I just
can’t format the string.

file-link = Dir.pwd, “#{filename}”, “#{mp3-title}”, “#{mp3-artist}”

In addition to all the suggestions above, there is the special case of
concatenating parts of a file path:

file_link = File.join(Dir.pwd, filename, mp3_title, mp3_artist)

Just in case that’s what you’re trying to achieve…

Cheers,
lasitha


#10

Pascal J. Bourguignon wrote:

hash-table, an array, a list, etc. For example:

filename = “/tmp/example.mp3”
mp3_title = “Example”
mp3_artist = “Musician”

file_link = [ Dir.pwd, filename, mp3_title, mp3_artist ]

file_link = Dir.pwd, filename, mp3_title, mp3_artist

file_link[0]
file_link[1]

file_link = { :directory => Dir.pwd,
:file => filename,
:title => mp3_title,
:artist => mp3_artist }

file_link = { :directory, Dir.pwd,
:file , filename,
:title , mp3_title,
:artist , mp3_artist }

def Cons.list(*objects)
result = nil
objects . reverse . each { | object | result = Cons.new(object,result) }
result
end

def foo *objects
objects
end
==>nil
foo( 2, 4, 9 )
==>[2, 4, 9]

but I am missing something in the format. See example
file-link = Dir.pwd, “#{filename}”, “#{mp3-title}”, “#{mp3-artist}”

So, assuming you want to build a new string formated with these
elements, separated by commas, you can do that with sprintf:

file_link = sprintf("%s, %s, %s, %s", Dir.pwd, filename, mp3_title, mp3_artist )

file_link = [Dir.pwd, filename, mp3_title, mp3_artist].join(", ")


#11

A struct would do nicely here.

FileLink = Struct.new( :directory, :filename, :title, :artist)
FileLink.new(Dir.pwd, “filename”, “title”, “artist”)

Jean-Michel