• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

ruby send call 的使用

武飞扬头像
许墨の小蝴蝶
帮助1

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

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

  1.  
    m = 12.method(" ")
  2.  
    # => `method` gets the ` ` method defined in the `Fixnum` instance
  3.  
    # m.class
  4.  
    # => Method
  5.  
     
  6.  
    m.call(3) #=> 15
  7.  
    # `3` is passed inside the ` ` method as argument
  8.  
    m.call(20) #=> 32

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

  1.  
    def say_hi
  2.  
     
  3.  
      puts "hihi"
  4.  
     
  5.  
    end
  6.  
     
  7.  
    def say_goodbye
  8.  
     
  9.  
      puts "goodbye"
  10.  
     
  11.  
    end
  12.  
     
  13.  
    # 使用send方式调用方法
  14.  
     
  15.  
    name = "say_hi"
  16.  
     
  17.  
    send(name)
  18.  
     
  19.  
    m = 12.method(" ")
  20.  
     
  21.  
    # => `method` gets the ` ` method defined in the `Fixnum` instance
  22.  
     
  23.  
    # m.class
  24.  
     
  25.  
    # => Method
  26.  
     
  27.  
    m.call(3)    #=> 15
  28.  
     
  29.  
    puts m.call(3)
  30.  
     
  31.  
    # `3` is passed inside the ` ` method as argument
  32.  
     
  33.  
    m.call(20)   #=> 32
  34.  
     
  35.  
    puts m.call(20)
学新通

运行:ruby hi.rb

得到输出:

  1.  
    hihi
  2.  
     
  3.  
    15
  4.  
     
  5.  
    32

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgifjeb
系列文章
更多 icon
同类精品
更多 icon
继续加载