Ruby 中未定义的方法错误

发布于 2024-12-04 20:52:36 字数 273 浏览 0 评论 0原文

下面的例子是我的教授在课堂上展示的,它工作得很好并且打印出来,

def printv(g)
  puts g.call("Fred")
  puts g.call("Amber")
end

printv(method(:hello))

>>hello Fred

  hello Amber

但是当我尝试在我的 irb/RubyMine 上运行它时,它显示了未定义的方法错误。我正在尝试他在课堂上展示的确切代码。我缺少什么?

谢谢!

The following example was showed by my professor in class and it worked perfectly fine and printed

def printv(g)
  puts g.call("Fred")
  puts g.call("Amber")
end

printv(method(:hello))

>>hello Fred

  hello Amber

but when I am trying to run it on my irb/RubyMine its showing undefined method error. I am trying the exact code what he showed in class. What am I missing?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-12-11 20:52:36

如果您查看 printv 的代码,您会发现 g 必须提供一个 call 方法。 Ruby 中有几个类默认提供 call 方法,其中包括 procs 和 lambda:

hello = lambda { |name| puts "Hello, #{name}" }
printv(hello) # prints: Hello, Fred and Hello, Amber

这里 hello 是一个存储 lambda 的变量,因此您不需要符号 (:hello) 来引用它。

现在让我们看看方法method。根据文档,它“将命名方法查找为 obj 中的接收者,返回 Method 对象(或引发 NameError)”。它的签名是“obj.method(sym) → method”,这意味着它接受一个符号参数并返回一个 方法对象。如果您现在调用 method(:hello),您将收到文档中提到的 NameError,因为当前没有名为“hello”的方法。一旦您定义了一个,事情就会起作用:

def hello(name)
  "Hello, #{name}"
end
method(:hello) #=> #<Method: Object#hello>
>> printv(method(:hello)) # works as expected

这也解释了为什么您在对另一个答案的评论中提到的调用 printv(method("hello") 失败:method< /code> 尝试提取方法对象,但如果没有该名称的方法,则会失败(顺便说一句,字符串作为参数似乎可以工作,似乎 method 会实习生其参数以防万一)。

If you look at the code for printv, you'll see that g will have to provide a call method. There are several classes in Ruby that provide a call method by default, amongst them procs and lambdas:

hello = lambda { |name| puts "Hello, #{name}" }
printv(hello) # prints: Hello, Fred and Hello, Amber

Here hello is a variable storing a lambda, so you don't need a symbol (:hello) to reference it.

Now let's look at the method method. According to the docs it "[l]ooks up the named method as a receiver in obj, returning a Method object (or raising NameError)". It's signature is "obj.method(sym) → method", meaning it takes a symbol argument and returns a method object. If you call method(:hello) now, you'll get the NameError mentioned in the docs, since there is currently no method named "hello". As soon as you define one, things will work though:

def hello(name)
  "Hello, #{name}"
end
method(:hello) #=> #<Method: Object#hello>
>> printv(method(:hello)) # works as expected

This also explains why the call printv(method("hello") that you mention in your comment on the other answer fails: method tries to extract a method object, but fails if there is no method by that name (a string as argument seems to work by the way, seems like method interns its argument just in case).

听,心雨的声音 2024-12-11 20:52:36

您还需要定义方法“hello”。

def printv(g)
  puts g.call("Fred")
  puts g.call("Amber")
end

def hello(s)
   "hello #{s}"
end 

printv(method(:hello))

>>hello Fred

  hello Amber 

You'll need to define the method "hello" as well.

def printv(g)
  puts g.call("Fred")
  puts g.call("Amber")
end

def hello(s)
   "hello #{s}"
end 

printv(method(:hello))

>>hello Fred

  hello Amber 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文