Ruby 扩展IRB以允许;过滤“;找不到我选择的方法?

Ruby 扩展IRB以允许;过滤“;找不到我选择的方法?,ruby,irb,Ruby,Irb,在IRB启动时,我加载了很多自定义文件、模块和类来增强它 我有一门课,对我很有帮助,但我不知道如何自动调用它。在这个上下文中,自动意味着:当我启动IRB时,当我键入一些不是方法或变量的东西时,我想调用我自己选择的自定义方法 我大致知道,这可以通过救援,或使用方法_missing,但我不确定该做哪一个。如果我键入一个IRB不知道的方法,比如“foo”,有人能告诉我如何在IRB(或irbrc)中调用一个方法吗?这看起来会很快让人恼火,但下面是如何做到的。将此添加到您的.irbrc: def self

在IRB启动时,我加载了很多自定义文件、模块和类来增强它

我有一门课,对我很有帮助,但我不知道如何自动调用它。在这个上下文中,自动意味着:当我启动IRB时,当我键入一些不是方法或变量的东西时,我想调用我自己选择的自定义方法


我大致知道,这可以通过
救援
,或使用
方法_missing
,但我不确定该做哪一个。如果我键入一个IRB不知道的方法,比如“foo”,有人能告诉我如何在IRB(或irbrc)中调用一个方法吗?

这看起来会很快让人恼火,但下面是如何做到的。将此添加到您的
.irbrc

def self.method_missing (name, *args, &block)                                   
  puts "#{name} is not a method, ya goof"                                       
end
显然,更改方法定义的内容可以更改捕获丢失的方法时发生的情况

现在,无论何时调用irb中
main
对象上的方法,它都会捕获缺少的方法

>> foo
foo is not a method, ya goof
=> nil
这只适用于顶级方法调用。如果要捕获所有丢失的方法调用,请添加以下内容:

class Object
  def method_missing (name, *args, &block)
    puts "#{self.class}##{name} is not a method, ya goof"
  end
end
请记住,这将向您揭示许多失败的方法调用,而您可能根本不知道这些调用

$ irb
String#to_int is not a method, ya goof
>> foo
NilClass#to_ary is not a method, ya goof
Object#foo is not a method, ya goof
=> nil
>> [].bar
Array#bar is not a method, ya goof
=> nil

我认为从长远来看,这不会是你想要的生活方式,但这就是如何做到的。把你自己击倒

这看起来很快就会让人恼火,但下面是你的做法。将此添加到您的
.irbrc

def self.method_missing (name, *args, &block)                                   
  puts "#{name} is not a method, ya goof"                                       
end
显然,更改方法定义的内容可以更改捕获丢失的方法时发生的情况

现在,无论何时调用irb中
main
对象上的方法,它都会捕获缺少的方法

>> foo
foo is not a method, ya goof
=> nil
这只适用于顶级方法调用。如果要捕获所有丢失的方法调用,请添加以下内容:

class Object
  def method_missing (name, *args, &block)
    puts "#{self.class}##{name} is not a method, ya goof"
  end
end
请记住,这将向您揭示许多失败的方法调用,而您可能根本不知道这些调用

$ irb
String#to_int is not a method, ya goof
>> foo
NilClass#to_ary is not a method, ya goof
Object#foo is not a method, ya goof
=> nil
>> [].bar
Array#bar is not a method, ya goof
=> nil

我认为从长远来看,这不会是你想要的生活方式,但这就是如何做到的。把你自己击倒

这看起来很快就会让人恼火,但下面是你的做法。将此添加到您的
.irbrc

def self.method_missing (name, *args, &block)                                   
  puts "#{name} is not a method, ya goof"                                       
end
显然,更改方法定义的内容可以更改捕获丢失的方法时发生的情况

现在,无论何时调用irb中
main
对象上的方法,它都会捕获缺少的方法

>> foo
foo is not a method, ya goof
=> nil
这只适用于顶级方法调用。如果要捕获所有丢失的方法调用,请添加以下内容:

class Object
  def method_missing (name, *args, &block)
    puts "#{self.class}##{name} is not a method, ya goof"
  end
end
请记住,这将向您揭示许多失败的方法调用,而您可能根本不知道这些调用

$ irb
String#to_int is not a method, ya goof
>> foo
NilClass#to_ary is not a method, ya goof
Object#foo is not a method, ya goof
=> nil
>> [].bar
Array#bar is not a method, ya goof
=> nil

我认为从长远来看,这不会是你想要的生活方式,但这就是如何做到的。把你自己击倒

这看起来很快就会让人恼火,但下面是你的做法。将此添加到您的
.irbrc

def self.method_missing (name, *args, &block)                                   
  puts "#{name} is not a method, ya goof"                                       
end
显然,更改方法定义的内容可以更改捕获丢失的方法时发生的情况

现在,无论何时调用irb中
main
对象上的方法,它都会捕获缺少的方法

>> foo
foo is not a method, ya goof
=> nil
这只适用于顶级方法调用。如果要捕获所有丢失的方法调用,请添加以下内容:

class Object
  def method_missing (name, *args, &block)
    puts "#{self.class}##{name} is not a method, ya goof"
  end
end
请记住,这将向您揭示许多失败的方法调用,而您可能根本不知道这些调用

$ irb
String#to_int is not a method, ya goof
>> foo
NilClass#to_ary is not a method, ya goof
Object#foo is not a method, ya goof
=> nil
>> [].bar
Array#bar is not a method, ya goof
=> nil

我认为从长远来看,这不会是你想要的生活方式,但这就是如何做到的。把你自己击倒

我会选择一种不同的方法:

  • 将所有需要的方法放入模块而不是类中
  • 让对象
    包含此模块
  • 使用foo.rb启动irb以始终自动加载定义
foo.rb:

module Foo
  def my_method
    puts "aye"
  end
end


class Object
  include Foo
end

现在,每当您在irb中键入
my_method
,它都会调用您的方法。

我会选择不同的方法:

  • 将所有需要的方法放入模块而不是类中
  • 让对象
    包含此模块
  • 使用foo.rb启动irb以始终自动加载定义
foo.rb:

module Foo
  def my_method
    puts "aye"
  end
end


class Object
  include Foo
end

现在,每当您在irb中键入
my_method
,它都会调用您的方法。

我会选择不同的方法:

  • 将所有需要的方法放入模块而不是类中
  • 让对象
    包含此模块
  • 使用foo.rb启动irb以始终自动加载定义
foo.rb:

module Foo
  def my_method
    puts "aye"
  end
end


class Object
  include Foo
end

现在,每当您在irb中键入
my_method
,它都会调用您的方法。

我会选择不同的方法:

  • 将所有需要的方法放入模块而不是类中
  • 让对象
    包含此模块
  • 使用foo.rb启动irb以始终自动加载定义
foo.rb:

module Foo
  def my_method
    puts "aye"
  end
end


class Object
  include Foo
end

现在,只要你在irb中键入
my_method
,它就会调用你的方法。

这几乎是完美的。但我有一个问题:原始错误消息在哪里?例如,如果我在irb中键入“foo”,我会得到NameError:未定义的局部变量或main:Object的方法'foo',但当我使用method_missing时,该错误似乎已完全消失。您可以抛出一个
超级
(无括号)在方法定义的最后,如果您希望重新提升normal NoMethodError,这几乎是完美的。但我有一个问题:原始错误消息在哪里?例如,如果我在irb中键入“foo”,我会得到NameError:未定义的局部变量或main:Object的方法'foo',但当我使用method_missing时,该错误似乎已完全消失。您可以抛出一个
超级
(无括号)在方法定义的最后,如果您希望重新提升normal NoMethodError,这几乎是完美的。有一个问题