Unable to run regex on file content after putting it?

i_trope

I have created a simple class to handle opening, reading, and closing a file. In addition, I would like to run a regex on its contents to find a 4 digit date. However, when I run my code I get the following error:

file_class.rb:17:in `find_date': undefined method `match' for nil:NilClass (NoMethodError)
    from file_class.rb:24:in `<main>'

This error only occurs if I run the read_file method before it, which simply puts the file contents. I am not sure why doing so would result in such an error.

Below is my code:

class MyFile
  attr_reader :handle

  def initialize(filename)
    @handle = File.open(filename)
  end

  def read_file
    puts @handle.gets
  end

  def finished
    @handle.close
  end

  def find_date
    matching = @handle.gets.match(/\d{4}/)
    puts matching[0]
  end
end

f = MyFile.new('text.txt')
f.read_file
f.find_date
f.finished

Thanks for the help.

joews

I'm guessing your file had a single line of contents.

When you call gets on an open file handle, the handle returns the line it is currently looking at and moves its "cursor" down to the next line. After you've read the last line, gets will return nil.

Your class would be better (for a few reasons) if you read the file once and cache the contents, rather than caching the handle and attempting to read several times:

class MyFile
  attr_reader :contents

  def initialize(filename)
    File.open(filename) do |f|
      @contents = f.read
    end
  end

  def find_date
    matching = @contents.match(/\d{4}/)
    puts matching[0]
  end
end

This approach is better because:

  • You only need to read the file once.
  • You're reading the whole file at once, not one line at a time (File#read instead of File#gets).
  • Your class has better encapsulation - other code that wants to use it doesn't need to tell your class to read the file, then find a date, then close the file - all of the logic is internal to your class.
  • You need to write less code - attr_accessor makes contents available to calling code without you needing to write your own methods. This is good because it's quicker to write and, much more importantly, it's clearer to read.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Putting a file content into an sql query?

From Dev

Unable to see whole pdf content after indexing pdf file in ES

From Dev

Content change run after javascript

From Dev

Unable to match after backslash in a regex

From Dev

gradle properties file - after putting new property how to preserve comments?

From Dev

Unable to fill list with content of a file

From Dev

Putting tab content in the middle

From Dev

unable to run script on dynamically json parsed content

From Dev

Python regex unable to remove content between [%~ abcd ~%]

From Dev

Launching maven file - Unable to Run

From Dev

Wrong file content after the upload

From Dev

li:after in horizontal navigation bar keeps putting its content on a new line

From Dev

Not able to post new content after putting AWS S3 on my Heroku Ruby on Rails App

From Dev

Unable to decompress file after decrypt it

From Dev

Extract content after "=" and before "&", Regex expression in java

From Dev

cat lines into file after regex

From Dev

Unable to run application after installing on phone

From Dev

After installing JDK, unable to run .jar

From Dev

unable to run grunt after installing using npm

From Dev

Unable to upload a file inside my Content folder, after publishing my asp.net mvc web application to IIS

From Dev

Unable to upload a file inside my Content folder, after publishing my asp.net mvc web application to IIS

From Dev

Unable to PHP read a remote file's content

From Dev

Javascript : unable to read file content using XMLHttpRequest

From Dev

Putting content below AppBarLayout in a CoordinatorLayout

From Dev

putting fixed content to flexslider slideshow

From Dev

Putting content next to vertical navigation

From Dev

CSS putting menu inline with the content

From Dev

Putting json content into PHP variable

From Dev

Putting 2 Categories in a file

Related Related

  1. 1

    Putting a file content into an sql query?

  2. 2

    Unable to see whole pdf content after indexing pdf file in ES

  3. 3

    Content change run after javascript

  4. 4

    Unable to match after backslash in a regex

  5. 5

    gradle properties file - after putting new property how to preserve comments?

  6. 6

    Unable to fill list with content of a file

  7. 7

    Putting tab content in the middle

  8. 8

    unable to run script on dynamically json parsed content

  9. 9

    Python regex unable to remove content between [%~ abcd ~%]

  10. 10

    Launching maven file - Unable to Run

  11. 11

    Wrong file content after the upload

  12. 12

    li:after in horizontal navigation bar keeps putting its content on a new line

  13. 13

    Not able to post new content after putting AWS S3 on my Heroku Ruby on Rails App

  14. 14

    Unable to decompress file after decrypt it

  15. 15

    Extract content after "=" and before "&", Regex expression in java

  16. 16

    cat lines into file after regex

  17. 17

    Unable to run application after installing on phone

  18. 18

    After installing JDK, unable to run .jar

  19. 19

    unable to run grunt after installing using npm

  20. 20

    Unable to upload a file inside my Content folder, after publishing my asp.net mvc web application to IIS

  21. 21

    Unable to upload a file inside my Content folder, after publishing my asp.net mvc web application to IIS

  22. 22

    Unable to PHP read a remote file's content

  23. 23

    Javascript : unable to read file content using XMLHttpRequest

  24. 24

    Putting content below AppBarLayout in a CoordinatorLayout

  25. 25

    putting fixed content to flexslider slideshow

  26. 26

    Putting content next to vertical navigation

  27. 27

    CSS putting menu inline with the content

  28. 28

    Putting json content into PHP variable

  29. 29

    Putting 2 Categories in a file

HotTag

Archive