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
相关推荐
开开心心_Every4 天前
多功能抽奖软件:游戏化互动体验
数据库·游戏·微信·pdf·excel·语音识别·ruby
C++chaofan6 天前
JUC 中 synchronized 的底层实现原理解析——Monitor
java·开发语言·c++·rust·ruby·juc·字节码
linux修理工6 天前
vagrant vmware
运维·ruby·vagrant
予枫的编程笔记9 天前
从入门到精通:RabbitMQ全面解析与实战指南
java·开发语言·后端·rabbitmq·ruby
时空无限10 天前
EFK 中使用 ruby 和 javascript 脚本去掉日志中颜色字符详解
linux·javascript·elk·ruby
掘根10 天前
【消息队列项目】客户端四大模块实现
开发语言·后端·ruby
lhrimperial17 天前
RabbitMQ核心技术深度解析
分布式·rabbitmq·ruby
小鸡脚来咯17 天前
RabbitMQ详解(从入门到实战)
开发语言·后端·ruby
云和数据.ChenGuang20 天前
OpenEuler系统下RabbitMQ安装与基础配置教程
服务器·分布式·rabbitmq·ruby·数据库运维工程师·运维教程
云和数据.ChenGuang20 天前
Deepseek适配场景:OpenEuler系统下RabbitMQ安装与基础配置教程
分布式·rabbitmq·ruby