如何将参数传递给define_method? [英] How do you pass arguments to define_method?

查看:66
本文介绍了如何将参数传递给define_method?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将参数传递给使用define_method定义的方法,我该怎么做?

I would like to pass an argument(s) to a method being defined using define_method, how would I do that?

推荐答案

您传递给define_method的块可以包含一些参数.这就是您定义的方法接受参数的方式.当您定义一个方法时,您实际上只是在给该块起个昵称并在类中保留对其的引用.参数随块一起提供.所以:

The block that you pass to define_method can include some parameters. That's how your defined method accepts arguments. When you define a method you're really just nicknaming the block and keeping a reference to it in the class. The parameters come with the block. So:

define_method(:say_hi) { |other| puts "Hi, " + other }

这篇关于如何将参数传递给define_method?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆