Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to put multiple values into a variable.

Reply
Thread Tools

How to put multiple values into a variable.

 
 
Harry Nash
Guest
Posts: n/a
 
      02-28-2009
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-forum.com/.

 
Reply With Quote
 
 
 
 
Todd Benson
Guest
Posts: n/a
 
      02-28-2009
On Sat, Feb 28, 2009 at 9:37 AM, Harry Nash <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Tim Hunter
Guest
Posts: n/a
 
      02-28-2009
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.rubyforge.org/

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      02-28-2009
On Sat, Feb 28, 2009 at 9:54 AM, Jeff Schwab <(E-Mail Removed)> 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

 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      02-28-2009
Harry Nash <(E-Mail Removed)> 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__
 
Reply With Quote
 
Sai Hl
Guest
Posts: n/a
 
      02-28-2009
> 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 wd, :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 wd, :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-forum.com/.

 
Reply With Quote
 
Leo
Guest
Posts: n/a
 
      02-28-2009
> 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).

 
Reply With Quote
 
lasitha
Guest
Posts: n/a
 
      02-28-2009
On Sat, Feb 28, 2009 at 9:07 PM, Harry Nash <(E-Mail Removed)> 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

 
Reply With Quote
 
William James
Guest
Posts: n/a
 
      02-28-2009
Pascal J. Bourguignon wrote:

> Harry Nash <(E-Mail Removed)> 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 )

 
Reply With Quote
 
Jean-Michel
Guest
Posts: n/a
 
      03-01-2009
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 <(E-Mail Removed)> 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-forum.com/.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
how to put xml values into mysql database loyed.preetham Java 0 10-10-2008 09:36 AM
why does the following with Queue, q.put('\x02', True) not put itin the queue? Gabriel Rossetti Python 3 04-25-2008 03:41 PM
Put array values into a single variable =?Utf-8?B?aHV6eg==?= ASP .Net 4 12-06-2004 12:29 PM
put transforobject values into javabean Eirik Eldorsen Java 2 01-15-2004 02:24 PM



Advertisments