Ruby 方法本身工作,而不是嵌入到较大的脚本中

Ruby 方法本身工作,而不是嵌入到较大的脚本中,ruby,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) 但是当我在这个脚本中调用embedde

下面是方法及其调用:

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)
但是当我在这个脚本中调用embedded时,它给了我一个错误:

/Users/X/Documents/RUBY/ex26.rb:86:in:Ex25:Module NoMethodError的未定义的methodsecret_公式 from/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
首先,让我们将脚本压缩为:

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
您可能会注意到一个问题:您开始在模块定义中运行计算。在此上下文中,self是Ex25,因此调用secret\u formula就是查找Ex25.secret\u formula。但是您已经将该方法定义为一个仅使用def secret_公式的实例方法,因此找不到它。编辑使其成为一个模块方法def self.secret_公式,它将正常工作。此时,您可能不希望代码在模块定义中运行

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行看起来好像在模块外,但实际上模块直到最后一行意外结束时才关闭。也许您打算在不包含文件其余部分的情况下将其放在更高的位置,并且在没有def self的情况下,在模块之外定义secret_公式。?在这两种情况下,一点格式设置都会大有帮助