方法本身就可以工作,而不是嵌入更大的脚本中

时间:2016-03-12 22:13:32

标签: ruby

以下是方法及其调用:

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)

但是当我把它称为嵌入在这个脚本中时,它给了我这个错误:

Exp:/Users/X/Documents/RUBY/ex26.rb:86:in <module:Ex25>': undefined method secret_formula':模块(NoMethodError)     来自/Users/X/Documents/RUBY/ex26.rb:1:in`'

以下是错误行,然后是整个脚本:

beans, jars, crates = secret_formula(start_point)




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
    puts words
  end

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

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

  # 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(word)
    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

  def secret_formula(started)
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates
  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}"

puts
puts "----------"
puts


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

puts
puts "----------"
puts

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(sort_words)
Ex25.print_last_word(sorted_words)
sorted_words = Ex25.sort_sentenc(sentence)
Ex25.print_first_and_last(sentence)
Ex25.print_first_and_last_sorted(sentence)
end

1 个答案:

答案 0 :(得分:3)

对于Minimal, Complete, Verifiable example,让我们将您的脚本压缩为:

module Ex25
  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."
end

您可能会注意到一个问题:您已开始在模块定义中运行计算。在此上下文中,selfEx25,因此调用secret_formula正在寻找Ex25.secret_formula。但是您已将该方法定义为仅def secret_formula的实例方法,因此未找到该方法。编辑使其成为模块方法(def self.secret_formula),它将正常工作。那时,您可能不希望代码在模块定义中运行。

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

start_point = 10000
beans, jars, crates = Ex25.secret_formula(start_point)
puts "With a starting point of: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."

在这种情况下,正确缩进代码可能会让你失望。您的puts行看起来像是在模块之外,但实际上模块并未关闭,直到最后一行出现意外end。也许你打算将end提高到你取消缩进文件其余部分的位置,然后在模块之外定义secret_formula,而不是def self.?在任何一种情况下,一点格式化都会有很长的路要走。