How to put multiple values into a variable.

Discussion in 'Ruby' started by Harry Nash, Feb 28, 2009.

  1. Harry Nash

    Harry Nash Guest

    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/.
     
    Harry Nash, Feb 28, 2009
    #1
    1. Advertising

  2. Harry Nash

    Todd Benson Guest

    On Sat, Feb 28, 2009 at 9:37 AM, 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 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
     
    Todd Benson, Feb 28, 2009
    #2
    1. Advertising

  3. Harry Nash

    Tim Hunter Guest

    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/
     
    Tim Hunter, Feb 28, 2009
    #3
  4. Harry Nash

    Todd Benson Guest

    On Sat, Feb 28, 2009 at 9:54 AM, Jeff Schwab <> 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
     
    Todd Benson, Feb 28, 2009
    #4
  5. 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[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__
     
    Pascal J. Bourguignon, Feb 28, 2009
    #5
  6. Harry Nash

    Sai Hl Guest

    > 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-forum.com/.
     
    Sai Hl, Feb 28, 2009
    #6
  7. Harry Nash

    Leo Guest

    > 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).
     
    Leo, Feb 28, 2009
    #7
  8. Harry Nash

    lasitha Guest

    On Sat, Feb 28, 2009 at 9:07 PM, 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}"


    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
     
    lasitha, Feb 28, 2009
    #8
  9. 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 )
     
    William James, Feb 28, 2009
    #9
  10. Harry Nash

    Jean-Michel Guest

    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 <> 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/.
     
    Jean-Michel, Mar 1, 2009
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?aHV6eg==?=

    Put array values into a single variable

    =?Utf-8?B?aHV6eg==?=, Dec 6, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    3,195
    =?Utf-8?B?aHV6eg==?=
    Dec 6, 2004
  2. Eirik Eldorsen

    put transforobject values into javabean

    Eirik Eldorsen, Jan 14, 2004, in forum: Java
    Replies:
    2
    Views:
    348
    Eirik Eldorsen
    Jan 15, 2004
  3. loyed.preetham
    Replies:
    0
    Views:
    450
    loyed.preetham
    Oct 10, 2008
  4. Stefan Ram
    Replies:
    3
    Views:
    386
    Eric Sosman
    Apr 16, 2011
  5. Replies:
    1
    Views:
    96
Loading...

Share This Page