Sorting array of names

Discussion in 'Ruby' started by Jane Doe, Nov 16, 2009.

  1. Jane Doe

    Jane Doe Guest

    Hi all,
    I have a Ruby program that reads in a text file of first names and
    surnames and prints them out in alphabetical order. Now I'm trying to
    add the option: if a person has a middle name, to print that out too and
    if they don't, to just print out their first name and surname.
    I'm having issues when allowing a person to have a middle name or not
    though. I think it is a problem with my to_s method and when I create a
    new person. I have attached my ruby file, if anyone has any help or
    advice on how to start this please let me know.
    The text file of names is just like this, with the first entry having no
    middle name and the second one having a middle name: John,Doe
    Jane,Doe,Amy
    --
    Posted via http://www.ruby-forum.com/.
     
    Jane Doe, Nov 16, 2009
    #1
    1. Advertising

  2. Jane Doe

    Paul Smith Guest

    Paul Smith, Nov 16, 2009
    #2
    1. Advertising

  3. Jane Doe

    Jane O'Maley Guest

    Jane O'Maley, Nov 16, 2009
    #3
  4. Jane O'Maley wrote:
    > Ah I'm sorry when trying initially it came up with application error, I
    > didn't realise it was to be attached again. I saved it as a txt file
    > this time.


    def initialize fname, lname, mname=nil
    @fname = fname
    @lname = lname
    @mname = mname
    end

    # This makes the last name nil if it's not defined when you create the
    person.

    def to_s
    # easy-to-read solution
    temp = "#{@lname}, #{fname}"
    if !@mname.nil?
    temp += ", #{mname}"
    end
    return temp
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Aldric Giacomoni, Nov 16, 2009
    #4
  5. Jane Doe

    Jane O'Maley Guest

    > def initialize fname, lname, mname=nil
    > @fname = fname
    > @lname = lname
    > @mname = mname
    > end
    >
    > # This makes the last name nil if it's not defined when you create the
    > person.
    >
    > def to_s
    > # easy-to-read solution
    > temp = "#{@lname}, #{fname}"
    > if !@mname.nil?
    > temp += ", #{mname}"
    > end
    > return temp
    > end


    Thank you very much for your help. Having sorted this out I was able to
    find and fix some other problems I had with it too and can keep moving
    forward with it.
    --
    Posted via http://www.ruby-forum.com/.
     
    Jane O'Maley, Nov 16, 2009
    #5
    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. Paddy McCarthy
    Replies:
    3
    Views:
    726
    Anthony J Bybell
    Sep 24, 2004
  2. Bob
    Replies:
    1
    Views:
    390
    Lucas Tam
    Jul 30, 2004
  3. Lewis G. Pringle, Jr.
    Replies:
    0
    Views:
    608
    Lewis G. Pringle, Jr.
    Sep 30, 2003
  4. Craig
    Replies:
    0
    Views:
    450
    Craig
    Feb 9, 2004
  5. Carl
    Replies:
    0
    Views:
    527
Loading...

Share This Page