Ruby The Hard Way Ex 25

时间:2015-03-13 22:52:24

标签: ruby

我正在尝试Ruby The Hard Way Ex 25。我得到了这个命令的预期结果:

Ex25.print_first_word(words)
# => All

但是对于另一个命令,而不是预期的结果:

Ex25.print_last_word(words)
# => wait.

我收到此错误消息:

NoMethodError: undefined method `pop' for nil:NilClass
from /Users/Me/Documents/RUBY/ex25.rb:18:in `print_last_word'

有谁知道这可能指的是什么?

编辑:

这是我为练习25所做的:

module Ex25

    def Ex25.break_words(stuff)
        words = stuff.split(' ')
        return words
    end

        def Ex25.sort_words(words)
            return words.sort
        end

            def Ex25.print_first_word(words)
                word = words.shift
                puts word
            end

            def Ex25.print_last_word(words)
                word = word.pop
                puts word
            end

def Ex25.sort_sentence(sentence)
    words = Ex25.break_words(sentence)
    return Ex25.sort_words(words)
end


def Ex25.print_first_and_last(sentence)
    words = Ex25.break_words(sentence)
    Ex25.print_first_word(words)
    Ex25.print_last_word(words)
end

def Ex25.print_first_and_last_sorted(sentence)
    words = Ex25.sort_sentence(sentence)
    Ex25.print_first_word(words)
    Ex25.print_last_word(words)
end

end

这就是我在终端中放入的相关错误:

MiniBean:ruby me$ irb
2.2.0 :001 > require "./ex25.rb"
 => true 
2.2.0 :002 > sentence = "All good things come to those who wait."
 => "All good things come to those who wait." 
2.2.0 :003 > words = Ex25.break_words(sentence)
 => ["All", "good", "things", "come", "to", "those", "who", "wait."] 
2.2.0 :004 > words
 => ["All", "good", "things", "come", "to", "those", "who", "wait."] 
2.2.0 :005 > sorted_words = Ex25.sort_words(words)
 => ["All", "come", "good", "things", "those", "to", "wait.", "who"] 
2.2.0 :006 > sorted_words
 => ["All", "come", "good", "things", "those", "to", "wait.", "who"] 
2.2.0 :007 > Ex25.print_first_word(words)
All
 => nil 
2.2.0 :008 > Ex25.print_last_word(words)
NoMethodError: undefined method `pop' for nil:NilClass
    from /Users/me/Documents/RUBY/ex25.rb:18:in `print_last_word'
    from (irb):8
    from /Users/me/.rvm/rubies/ruby-2.2.0/bin/irb:11:in `<main>'
2.2.0 :009 > words
 => ["good", "things", "come", "to", "those", "who", "wait."] 
2.2.0 :010 > Ex25.print_first_word(sorted_words)
All
 => nil 
2.2.0 :011 > Ex25.print_last_word(sorted_words)
NoMethodError: undefined method `pop' for nil:NilClass
    from /Users/me/Documents/RUBY/ex25.rb:18:in `print_last_word'
    from (irb):11
    from /Users/me/.rvm/rubies/ruby-2.2.0/bin/irb:11:in `<main>'
2.2.0 :012 > sorted_words
 => ["come", "good", "things", "those", "to", "wait.", "who"] 
2.2.0 :013 > sorted_words = Ex25.sort_sentence(sentence)
 => ["All", "come", "good", "things", "those", "to", "wait.", "who"] 
2.2.0 :014 > sorted_words
 => ["All", "come", "good", "things", "those", "to", "wait.", "who"] 
2.2.0 :015 > Ex25.print_first_and_last(sentence)
All
NoMethodError: undefined method `pop' for nil:NilClass
    from /Users/me/Documents/RUBY/ex25.rb:18:in `print_last_word'
    from /Users/me/Documents/RUBY/ex25.rb:31:in `print_first_and_last'
    from (irb):15
    from /Users/me/.rvm/rubies/ruby-2.2.0/bin/irb:11:in `<main>'
2.2.0 :016 > Ex25.print_first_and_last_sorted(sentence)
All
NoMethodError: undefined method `pop' for nil:NilClass
    from /Users/me/Documents/RUBY/ex25.rb:18:in `print_last_word'
    from /Users/me/Documents/RUBY/ex25.rb:37:in `print_first_and_last_sorted'
    from (irb):16
    from /Users/me/.rvm/rubies/ruby-2.2.0/bin/irb:11:in `<main>'
2.2.0 :017 >

1 个答案:

答案 0 :(得分:0)

def Ex25.print_last_word(words)
  word = word.pop
  puts word
end

在Ruby中,局部变量是在解析时确定的。因此,在解析期间,Ruby会看到word的赋值,并将创建一个名为word的局部变量,该变量未定义但计算结果为nil。在运行时,Ruby将尝试使用赋值的右侧分配给word,它也引用word本身。由于尚未分配word,因此评估结果为nil,这导致popnil的调用,pop没有pop方法。

据推测,您打算在参数绑定words上调用word,而不是局部变量{{1}}。请注意,任何中途不错的Ruby IDE都可能会警告您未使用的参数。