Ruby不会将`downcase`或`split`识别为已定义的方法?

时间:2017-02-05 18:08:17

标签: ruby

问题在于以下代码:

#write your code here
def translate phrase
  phrase = phrase.downcase.split(/ /)
  phrase.collect! do |word|
    word = word.split(//)
    switched = false
    while switched == false
      word.each_index do |letter| 
        if word[letter] == ("a" || "e" || "i" || "o" || "u")
          switched = true
          word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
        end
      end
    end
  end
  return phrase.join(" ")
end

puts translate("chocolate cream")
#Should return "ocolatechay eamcray"

当我运行它时,Ruby只返回一个空行。因此,为了解决问题,我将定义加载到repl中。 repl返回以下错误:

NoMethodError: undefined method `downcase' for #<RubyVM::InstructionSequence:0x000000016e8f88>
    from /home/adc/odin-project/web-development-101/21-ruby-tdd/ruby_tdd_project/learn_ruby/04_pig_latin/pig_latin.rb:3:in `translate'
    from /home/adc/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'

如果我从代码中删除了downcase,则会收到相同的错误消息,但这次只有split

这里有什么问题? (我非常确信downcasesplit 问题。)

2 个答案:

答案 0 :(得分:1)

如果返回一个空白字符串,因为您不会在collect!块内返回任何内容。返回word,它会起作用:

def translate phrase
  phrase = phrase.downcase.split(/ /)
  phrase.collect! do |word|
    word = word.split(//)
    switched = false
    while switched == false
      word.each_index do |letter|
        if word[letter] == ("a" || "e" || "i" || "o" || "u")
          switched = true
          word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
        end
      end
    end
    word
  end
  return phrase.join(" ")
end

puts translate("chocolate cream")
#=> atechocolay amcreay

它看起来并不完全符合您的预期,但它仍然比空白字符串更好。

至于您在控制台中的奇怪错误消息,它似乎特定于REPL(可能是因为方法名称translate)。

答案 1 :(得分:1)

更新1 (我将如何编写此代码)

def translate phrase
  phrase = phrase.downcase.split(/ /)
  phrase.collect! do |word|
    word = word.split(//)

    word.each_index do |letter| 
      if ["a", "e", "i", "o", "u"].include?(word[letter])
        if letter == 0
          word = word[letter..-1].join + "ay"
        else
          word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
        end

        break
      end
    end

    word
  end

  return phrase.join(" ")
end

puts translate("chocolate cream")

更新2 (如果我对您的代码进行一些细微更改以使其有用)

def translate phrase
  phrase = phrase.downcase.split(/ /)
  phrase.collect! do |word|
    word = word.split(//)
    switched = false
    while switched == false
      word.each_index do |letter| 
        if ["a", "e", "i", "o", "u"].include?(word[letter])
          switched = true
          if letter == 0
            word = word[letter..-1].join + "ay"
          else
            word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
          end
          break
        end
      end
    end
    word
  end
  return phrase.join(" ")
end

puts translate("chocolate cream")

<强>解释

  1. "o"==("a" || "e" || "i" || "o" || "u")

    "o"== "a" || "o" == "e" || "o" == "i" || "o" == "o" || "o" == "u"

    陈述不一样。第一个是假的

    (因为("a" || "e" || "i" || "o" || "u")=="a""o"!="a"),因为第二个是真的。

  2. 您需要内部if语句

    if letter == 0
      word = word[letter..-1].join + "ay"
    else
      word = (word[letter..-1] + word[0..(letter-1)]).join + "ay"
    end
    

    因为当单词以任何提到的元音开头时

    word[letter..-1] + word[0..(letter-1)]

    语句将返回整个单词两次,导致字母等于0。

  3. 最后你需要在while循环之后返回word个对象。