Ruby (NoMethodError) 即使声明了方法
Ruby (NoMethodError) even when method is declared
我正在努力学习 ruby。
我卡在第 26 课了。Here 是文件的 link。这是错误所在的代码片段:
five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"
def secret_formula(started)
jelly_bens = started * 500
jars = jelly_beans / 1000
crate = jars / 100
return jelly_beans, jars, crates
end
start_point = 10000
beans, jars, crates = secret_formula(start_point)
这是控制台上的错误
ex26.rb:77:in `<module:Ex25>': undefined method `secret_formula' for Ex25:Module (NoMethodError)
from ex26.rb:1:in `<main>'
您的代码中有很多拼写错误,在您修正一个错误后,还会出现更多错误。至于它,您在第 30
行键入 ed
而不是 end
,应该删除文件末尾的 end
。
变量声明中有几个拼写错误。首先,您有 jelly_bens
(缺少 "a")而不是 jelly_beans
。其次,您有 crate
(单数)而不是 crates
(复数)。修复这些问题后,代码将正确执行:
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)
在其他地方找不到这个答案,所以我是这样解决的:
本练习中 def secret_formula(started)
与其他 def
的区别在于它们以模块名称开头:def Ex26.break_words(stuff)
所以你可以把这里的问题行重写为def Ex26.secret_formula(started)
现在应该可以使用了。
我认为您在 "secret_formula" 函数声明中遗漏了 self。
这是我的练习代码。
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(0)
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)
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(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_first_word(words)
Ex25.print_last_word(words)
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 self.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 = Ex25.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(words)
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
我正在努力学习 ruby。 我卡在第 26 课了。Here 是文件的 link。这是错误所在的代码片段:
five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"
def secret_formula(started)
jelly_bens = started * 500
jars = jelly_beans / 1000
crate = jars / 100
return jelly_beans, jars, crates
end
start_point = 10000
beans, jars, crates = secret_formula(start_point)
这是控制台上的错误
ex26.rb:77:in `<module:Ex25>': undefined method `secret_formula' for Ex25:Module (NoMethodError)
from ex26.rb:1:in `<main>'
您的代码中有很多拼写错误,在您修正一个错误后,还会出现更多错误。至于它,您在第 30
行键入 ed
而不是 end
,应该删除文件末尾的 end
。
变量声明中有几个拼写错误。首先,您有 jelly_bens
(缺少 "a")而不是 jelly_beans
。其次,您有 crate
(单数)而不是 crates
(复数)。修复这些问题后,代码将正确执行:
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)
在其他地方找不到这个答案,所以我是这样解决的:
本练习中 def secret_formula(started)
与其他 def
的区别在于它们以模块名称开头:def Ex26.break_words(stuff)
所以你可以把这里的问题行重写为def Ex26.secret_formula(started)
现在应该可以使用了。
我认为您在 "secret_formula" 函数声明中遗漏了 self。 这是我的练习代码。
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(0)
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)
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(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_first_word(words)
Ex25.print_last_word(words)
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 self.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 = Ex25.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(words)
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