Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing command execution survay #547

Closed
wants to merge 1 commit into from
Closed

Conversation

tompng
Copy link
Member

@tompng tompng commented Mar 18, 2023

This is a survay draft pull request for command implementation.

Description

There are two types of IRB's command.

  • Command called alone or with arguments
    • whereami
    • cws object
    • show_source 'Set#to_a'
    • $ Set#to_a (some command accepts non-ruby syntax)
  • Command mostly used with method chain
    • context.echo = false
    • app.host (rails console)
    • helper.javascript_url('application') (rails console)

What should IRB command be?

For the past years

Command is a ruby method installed into current workspace object.

After show_source Set#to_a $ Set#to_a is introduced

Comamnd is a ruby method.
Command is also a UNIX-command-like-instruction command_name free_format_args for commands that has transform_args.

Command definition in this survey

Command is a UNIX-command-like-instruction command_name free_format_args. defined in @EXTEND_COMMANDS.
There are some extension methods(≠command) defined as a method of ExtendCommandBundle and aliases are in @ALIASES.

Pros

We can remove transform_args.

# can execute command directly by `command_class.new.execute(args)` so transform_args seems circuitous approach to me
# Implementation is mostly a boilerplate like `def self.tranform_args(arg)=arg.dump`
command, args = line.split(' ', 2)
line = "#{command} #{command_class.transform_args(args)}" if command? && transform_args?
evaluate(line)

Workspace object will less polluted by installed methods.

(self.methods - Object.new.methods).size #=> 87 → 8

Some inconsistent behavior of command will improve

# result of these differs
show_cmds ['Set','#to_a'].join
tap{show_cmds ['Set','#to_a'].join}

# $ foobar` is converted to `show_source "foobar"` even if show_source is not installed to workspace
cws Object.new.tap{def _1.show_source(*a)=puts(a)}
$ hello world #=> "hello world"

Cons

  • Command cannot be called as a method anymore 10.times{show_source 'Set#to_a'}
  • Less flexibility. Ruby can call a method like a command style and changing command might not be a ruby way.
  • If extension method is not a command, should it be shown in show_cmds?
    • It should be shown for convenience
    • Maybe we should call them method-command. (naming...)
    • Need to make an api to define category and description for extension methods

Other possibility

Specify command type :command or :method in command definition

@EXTEND_COMMANDS = [
  { name: :irb_whereami, module: :Whereami, path: "path", type: :command, aliases: []},
  { name: :irb_context, module: :ModuleName, path: "path", type: :method, aliases: []},
]

This seems better for me, but we should consider how to make no breaking change.

@tompng tompng closed this Jan 25, 2024
@tompng tompng deleted the refine_command branch January 25, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant