首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rubykoan中未定义的方法`as_name

rubykoan中未定义的方法`as_name
EN

Stack Overflow用户
提问于 2021-01-04 06:58:19
回答 2查看 132关注 0票数 0

我正在做RubyKoan about_iteration part。但是,我在test_each_is_a_method_on_arrays测试中遇到了一个错误,我不知道如何解决;

代码语言:javascript
复制
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">

这是代码;

代码语言:javascript
复制
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 

这是完整的错误;

代码语言:javascript
复制
➜  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%)
EN

回答 2

Stack Overflow用户

发布于 2021-01-04 08:56:21

您正在使用的RubyKoans版本已经过时七年了,并且只支持 1.8、1.9和2.0。当前版本的RubyKoans支持Ruby2.7(但不支持3.0)。

票数 4
EN

Stack Overflow用户

发布于 2021-04-16 09:00:18

in_ruby_version块中定义了#as_name。将Ruby3添加到版本列表中为我做到了这一点。

代码语言:javascript
复制
 in_ruby_version("1.9", "2", "3") do
    def as_name(name)
      name.to_sym
    end
  end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65555781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档