ruby send call 的简单使用

refer: ruby on rails - What does .call do? - Stack Overflow

Ruby使用call 可以调用方法或者proc

复制代码
m = 12.method("+")
# => `method` gets the `+` method defined in the `Fixnum` instance
# m.class 
# => Method

m.call(3)    #=> 15
# `3` is passed inside the `+` method as argument 
m.call(20)   #=> 32

send方法也可以调用方法,在本地写一个测试:

复制代码
def say_hi

  puts "hihi"

end

def say_goodbye

  puts "goodbye"

end

# 使用send方式调用方法

name = "say_hi"

send(name)

m = 12.method("+")

# => `method` gets the `+` method defined in the `Fixnum` instance

# m.class

# => Method

m.call(3)    #=> 15

puts m.call(3)

# `3` is passed inside the `+` method as argument

m.call(20)   #=> 32

puts m.call(20)

运行:ruby hi.rb

得到输出:

复制代码
hihi

15

32
相关推荐
柒儿吖10 小时前
官方适配完的命令行ruby在鸿蒙PC上的使用方法
开发语言·ruby·harmonyos
兩尛3 天前
高频提问部分
开发语言·后端·ruby
深蓝电商API3 天前
企业级爬虫架构设计:任务调度、容错、重试、降重
开发语言·爬虫·ruby
梁正雄5 天前
linux服务-RabbitMQ 原理与安装
linux·rabbitmq·ruby
互亿无线明明6 天前
国际短信通知服务:如何为全球业务构建稳定的跨国消息触达体系?
java·c语言·python·php·objective-c·ruby·composer
Hui Baby7 天前
Mq扩充队列提高并发
开发语言·后端·ruby
老虎062710 天前
RabbitMQ(RabbitMQ的消息收发的模板工具:SpringAMQP)
分布式·rabbitmq·ruby
songgz12 天前
洋葱式双向解析器演示(Ruby)
开发语言·后端·ruby
e***985715 天前
RabbitMQ HAProxy 负载均衡
rabbitmq·负载均衡·ruby
m***923815 天前
RabbitMQ 客户端 连接、发送、接收处理消息
分布式·rabbitmq·ruby