Ruby未定义的方法

时间:2015-07-21 17:28:09

标签: ruby

我正在做一个ruby练习,当我运行这个脚本时,首先它说&#34; 26.rb:94:语法错误,意外的输入结束,期待keyword_end&#34;,所以我添加了&# 34;端&#34;在最后一行。然后它说&#34; 26.rb:77:在<module:Ex25>': undefined method secret_formula&#39;对于Ex25:模块(NoMethodError)&#34;。我认为函数secret_formula很好吗?

谢谢!

module Ex25

  # This function will break up words for us.
  def Ex25.break_words(stuff)
    words = stuff.split(' ')
    return words
  end

  # Sorts the words.
  def Ex25.sort_words(words)
    return words.sort
  end

  # Prints the first word after popping it off.
  def Ex25.print_first_word(words)
    word = words.pop(1)
    puts word
  end

  # Prints the last word after popping it off.
  def Ex25.print_last_word(words)
    word = words.pop
    puts word
  end

  # Takes in a full sentence and returns the sorted words.
  def Ex25.sort_sentence(sentence)
    words = Ex25.break_words(sentence)
    return Ex25.sort_words(words)
  ed

  # Prints the first and last words of the sentence.
  def Ex25.print_first_and_last(sentence)
    words = Ex25.break_words(sentence)
    Ex25.print_first_word(words)
    Ex25.print_last_word(words)
  end

  # Sorts the words then prints the first and last one.
  def Ex25.print_first_and_last_sorted(sentence)
    words = Ex25.sort_sentence(sentence)
    Ex25.print_fist_word(words)
    Ex25.print_last_word(words)
  end
end


puts "Let's practice everything."
puts 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = <<END
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
END

puts "--------------"
puts poem
puts "--------------"


five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"

def secret_formula(started)
  jelly_beans = started * 500
  jars = jelly_beans / 1000
  crates = jars / 100
  return jelly_beans, jars, crates
end


start_point = 10000
beans, jars, crates = secret_formula(start_point)

puts "With a starting point of\: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."

start_point = start_point / 10

sentence = "All good things come to those who wait."
words = Ex25.break_words(sentence)
sorted_words = Ex25.sort_words(words)
Ex25.print_first_word(wrds)
Ex25.print_last_word(words)
Ex25.print_first_word(sorted_words)
Ex25.print_last_word(sorted_words)
sorted_words = Ex25.sort_sentence(sentence)
Ex25.print_first_and_last(sentence)
Ex25.print_first_and_last_sorted(sentence)

END

2 个答案:

答案 0 :(得分:3)

当您收到语法错误时,首先要做的是告诉Ruby解释具有最大警告的文件。我将代码保存到&#34; test.rb&#34;让Ruby看一下:

>ruby -cwW2 ~/Desktop/test.rb
warning: statement not reached
warning: mismatched indentations at 'end' with 'def' at 27
syntax error, unexpected end-of-input, expecting keyword_end

在这种情况下,

warning: mismatched indentations at 'end' with 'def' at 27

指向代码中未正确终止的方法。

有时候翻译很困惑,不能给你一个特定的行,但总的来说这是最好的起点。

以下是标志定义:

-c              check syntax only
-w              turn warnings on for your script
-W[level=2]     set warning level; 0=silence, 1=medium, 2=verbose

答案 1 :(得分:1)

你有一个错误的&#34;结束&#34;在这种方法中:

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