SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    why is there an error in my counter method?

    Code:
    class BrowserCount
      def initialize(file =  "browsers.txt")
        @file_lines = Array.new
        @file = file
      end
      
      def counter
        read_file
        @hash = Hash.new
        @files_lines.uniq.each do |line|
        count = 0
          @file_lines.each do |x|
            if x == line
              count +=1
            end
          end
          @hash[line]=count
        end
        @hash
      end
      
      private
      
      def read_file
        if @file_lines.empty?
        @open_file = File.open(@file)
        @file_lines = @open_file.readlines()
        end
      end
    end
    B = BrowserCount.new
    B.counter
    I don't understand why there is an error with @file_lines in my counter method. The error is: browser_count.rb:10:in `counter': undefined method `uniq' for nil:NilClass (NoMethodError) from browser_count.rb:32

  2. #2
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You set a @file_lines variable; on 10 you're looking for @files_lines.

  3. #3
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but I call read_file

  4. #4
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @files_lines.uniq.each do |line|

    vs.

    @file_lines = @open_file.readlines()


    file_lines != files_lines


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •