我正在做RubyKoan about_iteration part。但是,我在test_each_is_a_method_on_arrays测试中遇到了一个错误,我不知道如何解决;
undefined method `as_name' for #<AboutIteration:0x00007fcbc6031dc8 @name=:test_each_is_a_method_on_arrays,
@failure=#<NoMethodError: undefined method `as_name' for #<AboutIteration:0x00007fcbc6031dc8 ...>>, @koan_cou
nt=17, @step_count=158, @koan_file="AboutIteration">这是代码;
require File.expand_path(File.dirname(__FILE__) + '/neo')
class AboutIteration < Neo::Koan
# -- An Aside ------------------------------------------------------
# Ruby 1.8 stores names as strings. Ruby 1.9 stores names as
# symbols. So we use a version dependent method "as_name" to convert
# to the right format in the koans. We will use "as_name" whenever
# comparing to lists of methods.
in_ruby_version("1.8") do
def as_name(name)
name.to_s
end
end
in_ruby_version("1.9", "2.0") do
def as_name(name)
name.to_sym
end
end
# Ok, now back to the Koans.
# -------------------------------------------------------------------
def test_each_is_a_method_on_arrays
assert_equal true, [].methods.include?(as_name(:each))
end 这是完整的错误;
➜ koans git:(main) ✗ ruby path_to_enlightenment.rb
AboutIteration#test_each_is_a_method_on_arrays has damaged your karma.
The Master says:
You have not yet reached enlightenment.
Do not lose hope.
The answers you seek...
undefined method `as_name' for #<AboutIteration:0x00007fcbc6031dc8 @name=:test_each_is_a_method_on_arrays,
@failure=#<NoMethodError: undefined method `as_name' for #<AboutIteration:0x00007fcbc6031dc8 ...>>, @koan_cou
nt=17, @step_count=158, @koan_file="AboutIteration">
Please meditate on the following code:
/Users/amirulasyraf/Documents/koans/about_iteration.rb:27:in `test_each_is_a_method_on_arrays'
when you lose, don't lose the lesson
your path thus far [............................X_____________________] 157/278 (56%)发布于 2021-01-04 08:56:21
您正在使用的RubyKoans版本已经过时七年了,并且只支持 1.8、1.9和2.0。当前版本的RubyKoans支持Ruby2.7(但不支持3.0)。
发布于 2021-04-16 09:00:18
在in_ruby_version块中定义了#as_name。将Ruby3添加到版本列表中为我做到了这一点。
in_ruby_version("1.9", "2", "3") do
def as_name(name)
name.to_sym
end
endhttps://stackoverflow.com/questions/65555781
复制相似问题