From: William James Date: 2009-03-01T07:59:10+09:00 Subject: Re: How to put multiple values into a variable. Pascal J. Bourguignon wrote: > Harry Nash 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 )