[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to put multiple values into a variable.

Harry Nash

2/28/2009 3:38:00 PM

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}"
--
Posted via http://www.ruby-....

10 Answers

Todd Benson

2/28/2009 3:51:00 PM

0

On Sat, Feb 28, 2009 at 9:37 AM, Harry Nash <hjnash@adistantstar.com> 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

Tim Hunter

2/28/2009 3:52:00 PM

0

Harry Nash 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}"

--
RMagick: http://rmagick.ruby...

Jeff Schwab

2/28/2009 3:55:00 PM

0

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

Todd Benson

2/28/2009 4:04:00 PM

0

On Sat, Feb 28, 2009 at 9:54 AM, Jeff Schwab <jeff@schwabcenter.com> wrote:
> Harry Nash 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.

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

pjb

2/28/2009 4:07:00 PM

0

Harry Nash <hjnash@adistantstar.com> 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.


--
__Pascal Bourguignon__

Cyril Beer

2/28/2009 5:29:00 PM

0

> 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"
--
Posted via http://www.ruby-....

-lim-

2/28/2009 5:44:00 PM

0

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

lasitha

2/28/2009 8:49:00 PM

0

On Sat, Feb 28, 2009 at 9:07 PM, Harry Nash <hjnash@adistantstar.com> 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

William James

2/28/2009 10:56:00 PM

0

Pascal J. Bourguignon wrote:

> Harry Nash <hjnash@adistantstar.com> 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 = 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 }


> 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


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


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


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

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

Jean-Michel

3/1/2009 7:32:00 PM

0

A struct would do nicely here.

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

Jean-Michel


On Feb 28, 7:37 am, Harry Nash <hjn...@adistantstar.com> 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}"
> --
> Posted viahttp://www.ruby-....