#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Problem working with a file.txt


    Hi, I'm learning Ruby with a book called "Learn to program" by Chris Pine.
    The problem comes at doing a program that returns you when is the birthday of someone and his age after you type his name which is saved in a text file called "birthdays.txt". When I run it I have the following error on the Command Prompt: "'<main>' : undefined method '[]' for nil:NilClass <NoMethod Error>". The textfile it's compound of a bunch of lines that look like this: "Christopher Alexander, Oct 04, 1936"

    The part of the program that's wrong it's in the last line:
    PHP Code:
    filename 'birthdays.txt'
    birth_dates = { }

    File.open filename do |f
    f.each_line do |entry|
    date entry[-12..-1]
    name entry[0..-15
    birth_dates[name] = date
    end
    end

    puts 
    'Enter a name and their next birthday will be returned'
    name gets.chomp

    date 
    birth_dates[name]
    year date[-4..-1].to_i 
    Hope you can help me, thanks.

    PD: By the way, I'm about to finish this first book of programming, would you be so kind of recommend me another book that you consider useful at my stage? Thanks again
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    without concrete info, we won't be able to help you. I mean, we're flying blind here: We don't have the file, we don't have the input, you didn't check the variables, we can't see the behaviour of the program in action. So it could be pretty much anything.

    You should at least do a p birth_dates to see what Ruby read from the file.

    The underlying problem is that your code is extremely fragile, and at the same time you have no error checking. So any unexpected behaviour will end up with this NoMethodError.

    You should also get rid of this low-level programming style. Ruby is pretty smart, it rarely requires you to write down every small step.

    Let's assume you separate the data by tabs instead of hard-coding the particular positions:

    Code:
    filename = 'birth.txt'
    
    birth_dates = {}
    File.foreach filename do |line|
      next if line.strip.empty? # skip empty lines
      name, date =
        line.split("\t")
      birth_dates[name] = date
    end
    
    puts 'Enter a name and their next birthday will be returned'
    name = gets.rstrip
    
    if birth_dates.has_key? name
      puts "#{name} was born on #{birth_dates[name]}"
    else
      puts "Sorry, we don't know #{name}"
    end
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    Hi, thanks for replying Jacques. Look, I do know now where the problem is. The way Ruby reads the text file is the wrong part. The text file is something like this:
    Christopher Alexander, Oct 04, 1936
    Christopher Lambert, Mar 29, 1957
    Christopher Lee, May 27, 1922
    Christopher Lloyd, Oct 22, 1938
    Christopher Pine, Aug 03, 1976
    Christopher Plummer, Dec 13, 1927
    Christopher Walken, Mar 31, 1943
    When I do p birth_dates this is what I got with your code:
    {"Christopher Alexander,"=>"Oct 04, 1936\n", "Christopher Lambert,"=>"Mar 29, 19
    57\n", "Christopher Lee,"=>"May 27, 1922\n", "Christopher Lloyd,"=>"Oct 22, 1938
    \n", "Christopher Pine,"=>"Aug 03, 1976\n", "Christopher Plummer,"=>"Dec 13, 192
    7\n", "Christopher Walken,"=>"Mar 31, 1943"}
    So, the problem now is the comma that appears next to the name (which can be solved by deleting that comma from the text file) and the "\n" that appears after all the dates but the last one, how could you solve that?

    What would you recommend me to improve my skills in programming? Any book, webpage,...? Thanks
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    The newline characters are no real problem as long as you don't read from specific positions (which you shouldn't do, anyway). If you're afraid that they mess up your formatting, use String#rstrip or String#chomp -- just like you did with the user input.

    Originally Posted by lo_p_ez
    What would you recommend me to improve my skills in programming? Any book, webpage,...? Thanks
    The Ruby book that usually gets recommended is "Programming Ruby. The Pragmatic Programmer's Guide" by Dave Thomas.

    I haven't read it myself, though. I read "The Ruby Programming Language" by Flanagan as a short reference and then simply dived right in. The rest I learned from trying things out, reading the method reference (to find out new functionalities), reading through different forums, looking at other people's code etc.

    Comments on this post

    • lo_p_ez agrees : Thanks for all!!!
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo