【发布时间】:2013-09-09 17:51:54
【问题描述】:
def secret_formala(PH)
jelly_beans = PH * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
end
start_point = 10000
beans, jars, crates = secret_formala(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 "We can also do that this way:"
puts "We'd have %s beans, %s jars, and %s crates." % secret_formala(start_point)
puts
所以,这是我的代码和我的困惑,这对其他人来说可能看起来很明显,但由于我对 Ruby 还是很陌生,所以它逃脱了我。我不明白的是第 1 行和第 2 行“def secret_formula(PH)”和“jelly_beans = PH * 500”
我可以用任何东西代替“PH”并且它可以工作,但是代码如何理解我有“PH”的地方正在使用“start_point”数字?为什么我没有收到错误?在第 1 行的“def secret_formula(PH)”之后的“()”中添加任何内容有什么意义?
【问题讨论】:
-
请注意,在 Ruby 中,以大写字母开头的变量被视为常量。 (
PH在你的情况下) -
很难准确理解您的要求。您是在问,基本上,方法是如何工作的?