0

I got a basic ruby question. Did some tutorials a while ago but can't seem to wrap my mind about this problem:

class DependencyReader
   @tree
   @dependenciesFromFile

  def read_file
    @tree = {  }
    file = File.new("resources/2.Resource.txt", "r")
    @dependenciesFromFile = Hash.new

    while (line = file.gets)
      splitLine = line.split

      firstLetter = splitLine.shift
      firstLetter.downcase!

      depenendenciesInRow = splitLine
      @dependenciesFromFile[firstLetter] = depenendenciesInRow

    end

    get_tree 'a'
    file.close
  end

  def get_tree(letter)

    unless @tree.has_key? letter
      #add the index to that tree
      @tree[letter] = Array.new
    end

    puts @tree

    @tree.each do |key, value|
      value.push(@dependenciesFromFile[letter])
    end

    puts @dependenciesFromFile
    #prints
    # {"a"=>["B", "C"], "b"=>["C"], "c"=>["D", "E"], "d"=>["F"]}
    puts @dependenciesFromFile["a"]
    #prints nothing

    #gives error
    @dependenciesFromFile["a"].each do |key, value|
      get_tree value
    end

    puts @tree

  end
end

The problem occurs here:

puts @dependenciesFromFile
#prints
# {"a"=>["B", "C"], "b"=>["C"], "c"=>["D", "E"], "d"=>["F"]}
puts @dependenciesFromFile["a"]
#prints nothing

#gives error
@dependenciesFromFile["a"].each do |key, value|
  get_tree value
end

It's certainly filled but when I try to read it with a string as key. It gives nothing back.

After that the .each method gives error because it can't loop on Nil

Edit:

The code out of another class that invokes the read_file method, which calls on his turn the get_tree method.

reader = DependencyReader.new

reader.read_file
Puck
  • 2,080
  • 4
  • 19
  • 30
Sjaak Rusma
  • 1,424
  • 3
  • 23
  • 36
  • It's better if you write out the error it gives and not have us look through comments. – squiguy Apr 22 '14 at 18:47
  • You are right. Did that! – Sjaak Rusma Apr 22 '14 at 18:50
  • 1
    What are those spurious class instance variables that do nothing at the top of the class declaration? – Dave Newton Apr 22 '14 at 18:52
  • I share those among the read_file and the get_tree methods. It needs to be updated continously – Sjaak Rusma Apr 22 '14 at 18:54
  • 1
    After you get this code fixed, I seriously suggest you post the fixed code in http://codereview.stackexchange.com/ - it needs some serious review... – Uri Agassi Apr 22 '14 at 20:00
  • Thanks for the tip. Can not fix it tough.. Why does this not work in Ruby?? hash = {"a"=>["B", "C"], "b"=>["C"], "c"=>["D", "E"], "d"=>["F"]} puts hash["a"] – Sjaak Rusma Apr 22 '14 at 20:03
  • It is just a Nil class in my test script. Very very strange this – Sjaak Rusma Apr 22 '14 at 20:07
  • Sjaak, `puts hash["a"]` correctly returns `nil` after printing `"B"` and `"C"`. (You forgot a semi-colon between `hash = {...}` and `puts hash["a"]`.) – Cary Swoveland Apr 22 '14 at 20:18
  • I suggest close this question, since your real problem is found and fixed in http://stackoverflow.com/questions/23229232/why-is-reading-an-array-out-of-an-hash-not-possible-in-ruby - the extra code and discussion on this Q are not really adding anything. But the real answer is quite interesting/useful – Neil Slater Apr 22 '14 at 21:14

1 Answers1

0

In the code segment with the error, shouldn't you be using arguments passed instead of using "a", like following:

puts @dependenciesFromFile
#prints
# {"a"=>["B", "C"], "b"=>["C"], "c"=>["D", "E"], "d"=>["F"]}
puts @dependenciesFromFile[letter]
#should print a array

#should resolve error
@dependenciesFromFile[letter].each do |key, value|
  get_tree value
end
Saurabh
  • 71,488
  • 40
  • 181
  • 244
  • Thanks for your answer, but as you can see 'a' is exactly what's passed. I left it this way for debugging purposes. Before this error occured this was indeed the code that shouldve done the job – Sjaak Rusma Apr 22 '14 at 19:02