2012-02-19 4 views
0

上== bのOK、エラー==へ>:以下ルビー比較エラー。比較して、私が変更した場合、私はルビーで次のエラーを取得する> B

nano:jc] ruby ItemController.rb 

file read: snippets.txt 

ItemController.rb:23:in `read': undefined method `>' for nil:NilClass (NoMethodError) 
    from ItemController.rb:19:in `open' 
    from ItemController.rb:19:in `read' 
    from ItemController.rb:58 

は、苦情の原因となっているメソッドの定義です。それが動作

if line.index("<item>") == 0 

に沿っ

if line.index("<item>") > -1 

を参照してください。 > 0も失敗します。

ゆうっく!

def read 
    @item_count = 0 
    File.open(@file_name, 'r') do |f1| 
     while line=f1.gets 
     @line.concat([line]) 

     if line.index("<item>") > -1 
      puts "begin" 
      @item_count = @item_count + 1 
     end 

     if line.index("</item>") == 0 
      puts "end\n" 

     end 

     # puts line 
     end # while 
    end # do 
    end # def 

答えて

5

あなたline.index("<item>")nilに評価されます。 Nilは==メソッドを持っていますが、>はありません。根本的な原因は、あなたがそれを期待していなかったnilがあるということです。

+0

ありがとうございました!今、私は分かる。 –

+0

単にチェックマークをクリックしてください:http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – iltempo

関連する問題