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
相关推荐
Hellc0072 小时前
MacOS升级ruby版本
前端·macos·ruby
杰信步迈入C++之路1 天前
【RabbitMQ】快速上手
分布式·rabbitmq·ruby
hakesashou2 天前
ruby和python哪个好学
开发语言·python·ruby
谢尔登2 天前
【Node.js】RabbitMQ 不同交换器类型的使用
node.js·rabbitmq·ruby
谢尔登4 天前
【Node.js】RabbitMQ 延时消息
node.js·rabbitmq·ruby
kalvin_y_liu4 天前
RabbitMQ
分布式·rabbitmq·ruby
谢尔登4 天前
【Node.js】初识 RabbitMQ
node.js·rabbitmq·ruby
我是全栈架构师6 天前
Linux(CentOS8)服务器安装RabbitMQ
服务器·rabbitmq·ruby
fredricen6 天前
CentOS7下安装Ruby3.2.4的实施路径
ruby
Xwzzz_8 天前
RabbitMQ的Direct Exchange模式实现的消息发布案例
分布式·rabbitmq·ruby