• 找到了一个 workaround😀

    old = :+
    new = :plus__
    Fixnum.class_eval "alias #{new} #{old}"
    
  • 确实 alias_method 可以传变量,多谢指点!👍

  • 知道对 method 使用的,比如下面这段代码,想把 :old_plus 和 :+ 先赋给变量,再来使用 alias,就会报错。

    class Fixnum
      alias :old_plus :+
      def +(value)
        self.old_plus(value).old_plus(1)
      end
    end
    
  • new --> new_name 一样不行。

  • monkey patch 能有效吗? alias 是 ruby 的 keyword,不是 method。

  • 你的运行环境是怎样的呀?

  • 因为例子中的代码改写了 + 的结果为正确结果 +1, 因此当定义完 +(method)后,C:/Ruby22/lib/ruby/site_ruby/2.2.0/rbreadline.rb 里的表达式做加法运算时出现了对数组索引越界的错误。 但是为何有些环境下就正常运行呢(比如 Linux)?

  • 我的出错信息是这样的。当前在 Windows 上使用的是 ruby 2.2.6, 试过 ruby 2.4.2, 结果是一样的。是不是哪里配置不对?

    irb(main):001:0> class Fixnum
    irb(main):002:1> alias :old_plus :+
    irb(main):003:1* def +(value)
    irb(main):004:2> self.old_plus(value).old_plus(1)
    irb(main):005:2> end
    irb(main):006:1> end
    => :+
    C:/Ruby22/lib/ruby/site_ruby/2.2.0/rbreadline.rb:1716:in `block in expand_prompt': no implicit conversion of nil into St
    ring (TypeError)
            from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rbreadline.rb:1696:in `each'
            from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rbreadline.rb:1696:in `expand_prompt'
            from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rbreadline.rb:1784:in `rl_expand_prompt'
            from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rbreadline.rb:1809:in `rl_set_prompt'
            from C:/Ruby22/lib/ruby/site_ruby/2.2.0/rbreadline.rb:4821:in `readline'
            from C:/Ruby22/lib/ruby/site_ruby/2.2.0/readline.rb:45:in `readline'
            from C:/Ruby22/lib/ruby/2.2.0/irb/input-method.rb:150:in `gets'
            from C:/Ruby22/lib/ruby/2.2.0/irb.rb:469:in `block (2 levels) in eval_input'
            from C:/Ruby22/lib/ruby/2.2.0/irb.rb:623:in `signal_status'
            from C:/Ruby22/lib/ruby/2.2.0/irb.rb:468:in `block in eval_input'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:188:in `call'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:188:in `buf_input'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:103:in `getc'
            from C:/Ruby22/lib/ruby/2.2.0/irb/slex.rb:205:in `match_io'
            from C:/Ruby22/lib/ruby/2.2.0/irb/slex.rb:75:in `match'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:282:in `token'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:264:in `lex'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:235:in `block (2 levels) in each_top_level_statement'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:231:in `loop'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:231:in `block in each_top_level_statement'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:230:in `catch'
            from C:/Ruby22/lib/ruby/2.2.0/irb/ruby-lex.rb:230:in `each_top_level_statement'
            from C:/Ruby22/lib/ruby/2.2.0/irb.rb:485:in `eval_input'
            from C:/Ruby22/lib/ruby/2.2.0/irb.rb:395:in `block in start'
            from C:/Ruby22/lib/ruby/2.2.0/irb.rb:394:in `catch'
            from C:/Ruby22/lib/ruby/2.2.0/irb.rb:394:in `start'
            from C:/Ruby22/bin/irb:11:in `<main>'
    

    但是在 Linux 上试了 ruby 1.8.7,功能正常,输出如下,和上面的输出相比,=>:+ 变成了 => nil, 这又是什么原因?

    irb(main):001:0> class Fixnum
    irb(main):002:1> alias :old_plus :+
    irb(main):003:1* def +(value)
    irb(main):004:2> self.old_plus(value).old_plus(1)
    irb(main):005:2> end
    irb(main):006:1> end
    => nil
    
    
  • 谢谢指点! 通过对文档里的 inspect 方法的返回值(AClass.method 或者 AClass#method)进行判断,可以用来区分 class method 还是 instance method。 但是,如果要判断 public 还是 private,似乎没提供直接的方法。

  • ruby 中 == ?. 的用法解释 at 2017年09月08日

    经过验证,这段代码是查找类似 .dir_a 这样的以 dot 开头且跟有名字的目录,而对 . 目录本身不起作用。

    谢谢给位的指点!