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
相关推荐
2301_768350233 天前
RabbitMq快速入门程序
分布式·rabbitmq·ruby
FreeBuf_5 天前
攻击者利用Discord Webhook通过npm、PyPI和Ruby软件包构建隐蔽C2通道
前端·npm·ruby
i学长的猫5 天前
Ruby小白学习路线
开发语言·学习·ruby
i学长的猫5 天前
Ruby on Rails 从0 开始入门到进阶到高级 - 10分钟速通版
后端·ruby on rails·ruby
程序_白白13 天前
介绍一下什么是RabbitMQ的发送者可靠性?
分布式·rabbitmq·ruby
一只学java的小汉堡14 天前
Spring Cloud RabbitMQ 详解:从基础概念到秒杀实战
开发语言·后端·ruby
数字化顾问15 天前
“AMQP协议深度解析:消息队列背后的通信魔法”之核心概念与SpringBoot落地实战
开发语言·后端·ruby
会开花的二叉树16 天前
RabbitMQ C++ 客户端封装与实战
c++·rabbitmq·ruby
!chen16 天前
RabbitMQ的安装集群、镜像队列配置
分布式·rabbitmq·ruby
拾忆,想起16 天前
RabbitMQ事务机制深度剖析:消息零丢失的终极武器
java·开发语言·分布式·后端·rabbitmq·ruby