No implicit conversion of file into string

James

I am hoping to loop through a directory of json files and convert them into ruby hashes. My file looper function grabs every file successfully, and the files are in correct json format. Here is my code:

def self.update_server
  if Dir.exist?("log/order_errors") == true
    logger.debug "#{Dir.entries("log/order_errors")}"
    Dir.foreach("log/order_errors") { |f|
      logger.debug "Retrieved #{f}"
      File.open(f, "r") { |current_file|
        JSON.parse(current_file)
      }
    }
  else
    logger.error "Was unable to find the directory specified."
  end
end

Any idea of what is going on or what I need to do to tidy up my files so that they can be parsed correctly?

7stud

JSON.parse() takes a string as an argument--not a file:

require 'json'

File.open('data.json') do |f|
  JSON.parse(f)
end

--output:--
...no implicit conversion of File into String (TypeError)...

This is the way to do it:

require 'json'

File.open('data.json') do |f|
  hash = JSON.parse(f.read)  #***HERE***
  p hash
end

--output:--
{"x"=>1, "y"=>2}

The json module's docs are horrible, which unfortunately is typical of ruby. The docs say that the argument to parse() is a JSON document, which certainly sounds more like a File than a String. What the docs should say is that the argument needs to be a String in json format.

By the way, in this line:

if Dir.exist?("log/order_errors") == true 

...the exist?() method call is replaced by its return value, so if the directory exists ruby will convert that line to:

if true == true

Then ruby has to do the comparison true == true, and ruby replaces the comparison with the result of the comparison, i.e. true to produce this:

if true

Now, what if you wrote this instead:

if Dir.exist?("log/order_errors")

Once again, the exist?() method call is replaced by its return value, and if the directory exists, you will get this:

if true

If the directory doesn't exist, the exist?() method call is replaced by false, producing this:

if false

Therefore, writing == true after the exist?() method call is both a waste of time to type, and it wastes processing time because it requires ruby to do an extra comparison. You'll get the same result without doing the comparison, as shown above. The rule is: if a method returns true or false, you don't need to write == true after it. And in ruby it is usually pretty easy to tell when a method returns true or false because the method name will end with a ?.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

No implicit conversion of Array into String?

From Dev

No implicit conversion of Array into String?

From Dev

implicit conversion string to integer

From Dev

Can't open CSV file: no implicit conversion of StringIO into String

From Dev

No implicit conversion of HTTParty::Response into String

From Dev

no implicit conversion of Hash into String (TypeError)

From Dev

No implicit conversion of String into Integer (TypeError)?

From Dev

Implicit conversion works for symbol but not string

From Dev

no implicit conversion of nil into String error

From Dev

Rails: no implicit conversion of nil into String

From Dev

no implicit conversion of String into Integer (TypeError)

From Dev

no implicit conversion of Hash into String (TypeError)

From Dev

Rails: no implicit conversion of nil into String

From Dev

implicit conversion Option[T] to String

From Dev

TypeError: no implicit conversion of nil into String

From Dev

No implicit conversion of ActiveSupport::HashWithIndifferentAccess into String?

From Dev

No implicit conversion of Hash into String (Ruby)

From Dev

no implicit conversion of String into Hash (smarter_csv gem, uploading a file with an imput)

From Dev

C++ constructors and implicit string conversion

From Dev

Ruby - TypeError: no implicit conversion of Hash into String

From Dev

Ruby no implicit conversion of Fixnum into String (TypeError)

From Dev

ruby - json no implicit conversion of String into Integer (TypeError)

From Dev

Nested Attributes Error: no implicit conversion of String into Integer

From Dev

Chef attributes "no implicit conversion of String into Integer"

From Dev

Implicit String conversion in scala doesnt compile

From Dev

RSpec - Type error - no implicit conversion of String into Integer

From Dev

tinymce-rails no implicit conversion of String into Hash

From Dev

TypeError: no implicit conversion of nil into String error

From Dev

Rails 4 form - no implicit conversion of Model into String

Related Related

  1. 1

    No implicit conversion of Array into String?

  2. 2

    No implicit conversion of Array into String?

  3. 3

    implicit conversion string to integer

  4. 4

    Can't open CSV file: no implicit conversion of StringIO into String

  5. 5

    No implicit conversion of HTTParty::Response into String

  6. 6

    no implicit conversion of Hash into String (TypeError)

  7. 7

    No implicit conversion of String into Integer (TypeError)?

  8. 8

    Implicit conversion works for symbol but not string

  9. 9

    no implicit conversion of nil into String error

  10. 10

    Rails: no implicit conversion of nil into String

  11. 11

    no implicit conversion of String into Integer (TypeError)

  12. 12

    no implicit conversion of Hash into String (TypeError)

  13. 13

    Rails: no implicit conversion of nil into String

  14. 14

    implicit conversion Option[T] to String

  15. 15

    TypeError: no implicit conversion of nil into String

  16. 16

    No implicit conversion of ActiveSupport::HashWithIndifferentAccess into String?

  17. 17

    No implicit conversion of Hash into String (Ruby)

  18. 18

    no implicit conversion of String into Hash (smarter_csv gem, uploading a file with an imput)

  19. 19

    C++ constructors and implicit string conversion

  20. 20

    Ruby - TypeError: no implicit conversion of Hash into String

  21. 21

    Ruby no implicit conversion of Fixnum into String (TypeError)

  22. 22

    ruby - json no implicit conversion of String into Integer (TypeError)

  23. 23

    Nested Attributes Error: no implicit conversion of String into Integer

  24. 24

    Chef attributes "no implicit conversion of String into Integer"

  25. 25

    Implicit String conversion in scala doesnt compile

  26. 26

    RSpec - Type error - no implicit conversion of String into Integer

  27. 27

    tinymce-rails no implicit conversion of String into Hash

  28. 28

    TypeError: no implicit conversion of nil into String error

  29. 29

    Rails 4 form - no implicit conversion of Model into String

HotTag

Archive