模块 IRB::ExtendCommandBundle

安装默认的 irb 扩展命令包。

常量

NO_OVERRIDE

参见 install_alias_method.

OVERRIDE_ALL

参见 install_alias_method.

OVERRIDE_PRIVATE_ONLY

参见 install_alias_method.

公共类方法

all_commands_info() 点击切换源代码
# File lib/irb/extend-command.rb, line 206
def self.all_commands_info
  return @@commands unless @@commands.empty?
  user_aliases = IRB.CurrentContext.command_aliases.each_with_object({}) do |(alias_name, target), result|
    result[target] ||= []
    result[target] << alias_name
  end

  @EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases|
    if !defined?(ExtendCommand) || !ExtendCommand.const_defined?(cmd_class, false)
      require_relative load_file
    end

    klass = ExtendCommand.const_get(cmd_class, false)
    aliases = aliases.map { |a| a.first }

    if additional_aliases = user_aliases[cmd_name]
      aliases += additional_aliases
    end

    display_name = aliases.shift || cmd_name
    @@commands << { display_name: display_name, description: klass.description, category: klass.category }
  end

  @@commands
end
def_extend_command(cmd_name, cmd_class, load_file, *aliases) 点击切换源代码

在给定的 cmd_class Class 上评估给定的 cmd_name

还会为该方法定义任何给定的 aliases

可选的 load_file 参数将在方法定义中被要求。

# File lib/irb/extend-command.rb, line 259
def self.def_extend_command(cmd_name, cmd_class, load_file, *aliases)
  case cmd_class
  when Symbol
    cmd_class = cmd_class.id2name
  when String
  when Class
    cmd_class = cmd_class.name
  end

  line = __LINE__; eval %[
    def #{cmd_name}(*opts, **kwargs, &b)
      Kernel.require_relative "#{load_file}"
      ::IRB::ExtendCommand::#{cmd_class}.execute(irb_context, *opts, **kwargs, &b)
    end
  ], nil, __FILE__, line

  for ali, flag in aliases
    @ALIASES.push [ali, cmd_name, flag]
  end
end
extend_object(obj) 点击切换源代码

使用 install_alias_method 在给定对象上安装默认 irb 命令的别名方法。

调用超类方法
# File lib/irb/extend-command.rb, line 308
def self.extend_object(obj)
  unless (class << obj; ancestors; end).include?(EXCB)
    super
    for ali, com, flg in @ALIASES
      obj.install_alias_method(ali, com, flg)
    end
  end
end
install_extend_commands() 点击切换源代码

安装默认的 irb 命令。

# File lib/irb/extend-command.rb, line 247
def self.install_extend_commands
  for args in @EXTEND_COMMANDS
    def_extend_command(*args)
  end
end
load_command(command) 点击切换源代码

如果存在这样的命令,则将命令名称转换为其实现类

# File lib/irb/extend-command.rb, line 233
def self.load_command(command)
  command = command.to_sym
  @EXTEND_COMMANDS.each do |cmd_name, cmd_class, load_file, *aliases|
    next if cmd_name != command && aliases.all? { |alias_name, _| alias_name != command }

    if !defined?(ExtendCommand) || !ExtendCommand.const_defined?(cmd_class, false)
      require_relative load_file
    end
    return ExtendCommand.const_get(cmd_class, false)
  end
  nil
end

公共实例方法

install_alias_method(to, from, override = NO_OVERRIDE) 点击切换源代码

安装默认 irb 命令的别名方法,参见 ::install_extend_commands.

# File lib/irb/extend-command.rb, line 282
def install_alias_method(to, from, override = NO_OVERRIDE)
  to = to.id2name unless to.kind_of?(String)
  from = from.id2name unless from.kind_of?(String)

  if override == OVERRIDE_ALL or
      (override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or
      (override == NO_OVERRIDE) &&  !respond_to?(to, true)
    target = self
    (class << self; self; end).instance_eval{
      if target.respond_to?(to, true) &&
        !target.respond_to?(EXCB.irb_original_method_name(to), true)
        alias_method(EXCB.irb_original_method_name(to), to)
      end
      alias_method to, from
    }
  else
    Kernel.warn "irb: warn: can't alias #{to} from #{from}.\n"
  end
end
irb_context() 点击切换源代码

显示当前配置。

通过向 IRB.conf 发送消息来修改配置。

# File lib/irb/extend-command.rb, line 31
def irb_context
  IRB.CurrentContext
end
irb_exit(ret = 0) 点击切换源代码

退出当前 irb 上下文

ret 是可选的信号或消息,发送到 Context#exit

IRB.CurrentContext.exit 相同。

# File lib/irb/extend-command.rb, line 24
def irb_exit(ret = 0)
  irb_context.exit(ret)
end
irb_load(*opts, &b) 点击切换源代码

加载给定文件,类似于 Kernel#load,请参阅 IrbLoader#irb_load

# File lib/irb/ext/use-loader.rb, line 19
def irb_load(*opts, &b)
  ExtendCommand::Load.execute(irb_context, *opts, &b)
end
irb_require(*opts, &b) 点击切换源代码

加载给定文件,类似于 Kernel#require

# File lib/irb/ext/use-loader.rb, line 24
def irb_require(*opts, &b)
  ExtendCommand::Require.execute(irb_context, *opts, &b)
end