3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

irb が動くシンプルな何かを作る

Last updated at Posted at 2016-09-23

irb を起動する

sample1.rb
require 'irb'

IRB.start

上記ファイルを作ったら、 ruby コマンドで実行すると、irb が起動できます。

$ ruby sample1.rb 
irb(main):001:0>

モジュールを読み込む

ExtendCommandBundle ってのがあるので、読み込みたいモジュール名をincludeメソッドに渡します。

sample2.rb
require 'irb'

module FooModule
  def foo
    puts 'bar'
    'baz'
  end
end

console = IRB
console::ExtendCommandBundle.include(FooModule)
console.start

すると、module で定義したメソッドをirb 上で使えるようになります。

$ ruby sample2.rb 
irb(main):001:0> foo
bar
=> "baz"
irb(main):002:0>
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?