Skip to content

Commit

Permalink
Update how to integrate in irbrc
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed May 2, 2024
1 parent 9d6e61b commit 7dd1812
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 67 deletions.
2 changes: 2 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ updates:
patterns:
- '*rubocop*'
- package-ecosystem: 'bundler'
# For feature dependedncy updates,
# Keep this definition even if the /gemfiles/oldest/Gemfile is completely same as /Gemfile
directory: '/gemfiles/oldest'
schedule:
interval: 'weekly'
Expand Down
25 changes: 3 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,6 @@ $ irb

Then you can use `pa` as an IRB command.

```ruby
irb(main):001:0> pa %q{ "0".class == "3".to_i.times.map {|i| i + 1 }.class }
result: false

"0".class == "3".to_i.times.map {|i| i + 1 }.class
| | | | | |
| | | | | Array
| | | | [1, 2, 3]
| | | #<Enumerator: ...>
| | 3
| false
String
=> nil
```

The `pa` just takes strings of the code.

If you want to directly pass `expression`, [.irbrc](examples/.irbrc) is the hack for single line code.\
if you don't have the file yet, putting the file as one of your `$IRBRC`, `$XDG_CONFIG_HOME/irb/irbrc` or `$HOME/.irbrc`

Then you can use the `pa` as below...

```ruby
irb(main):001:0> pa "0".class == "3".to_i.times.map {|i| i + 1 }.class
result: false
Expand All @@ -83,8 +61,11 @@ result: false
=> nil
```

No hack is needed in your irbrc, thank you irb developers!

## References

- [power-assert-js/power-assert](https://github.com/power-assert-js/power-assert)
- [Power Assert in Ruby](https://speakerdeck.com/k_tsj/power-assert-in-ruby)
- [ja - IRB is new than Pry](https://k0kubun.hatenablog.com/entry/2021/04/02/211455)
- [Support command registration](https://github.com/ruby/irb/pull/886)
19 changes: 0 additions & 19 deletions examples/.irbrc

This file was deleted.

2 changes: 1 addition & 1 deletion gemfiles/oldest/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ group :test do
gem 'test-unit', '~> 3.6.2'
gem 'warning', '~> 1.3.0'

gem 'irb', '1.12.0'
gem 'irb', '1.13.0'
gem 'power_assert', '2.0.3'
end
2 changes: 1 addition & 1 deletion irb-power_assert.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Gem::Specification.new do |gem|
'rubygems_mfa_required' => 'true',
}

gem.add_runtime_dependency 'irb', '>= 1.12.0', '< 2.0'
gem.add_runtime_dependency 'irb', '>= 1.13.0', '< 2.0'
gem.add_runtime_dependency 'power_assert', '>= 2.0.3', '< 3.0'

gem.required_ruby_version = '>= 3.2'
Expand Down
10 changes: 1 addition & 9 deletions lib/irb/cmd/pa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,5 @@ def execute(expression)
end
end

if ::IRB::Command.respond_to?(:register)
Command.register(:pa, PaCommand)
else
Command::Pa = PaCommand

module ExtendCommandBundle
def_extend_command(:irb_pa, :Pa, __FILE__, [:pa, NO_OVERRIDE])
end
end
Command.register(:pa, PaCommand)
end
17 changes: 2 additions & 15 deletions test/test_irb_power_assert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,10 @@ def test_typical_example
EOD

out, err = capture_output do
if ::IRB::Command.respond_to?(:register)
# Since ruby 3.4 (or newer irb gem), no need irbrc hack to realize `pa exp` instead of `pa 'exp'`
execute_lines(%q{pa "0".class == "3".to_i.times.map {|i| i + 1 }.class})
else
IRB.setup(__FILE__, argv: [])
IRB.conf[:USE_MULTILINE] = true
IRB.conf[:USE_SINGLELINE] = false
IRB.conf[:VERBOSE] = false
workspace = IRB::WorkSpace.new(self)
irb = IRB::Irb.new(workspace)
IRB.conf[:MAIN_CONTEXT] = irb.context

irb.context.main.irb_pa(%q{"0".class == "3".to_i.times.map {|i| i + 1 }.class})
end
execute_lines(%q{pa "0".class == "3".to_i.times.map {|i| i + 1 }.class})
end

assert_equal('', err)
assert_equal((::IRB::Command.respond_to?(:register) ? expected + "=> nil\n" : expected), out)
assert_equal(expected + "=> nil\n", out)
end
end

0 comments on commit 7dd1812

Please sign in to comment.