LoginSignup
6
2

More than 5 years have passed since last update.

オフラインでもRubyドキュメントを確認できるriコマンドを使ってみた(●´ϖ`●)

Last updated at Posted at 2018-12-09

オフラインでもRubyドキュメントを確認できるriコマンドを使ってみた

あーこのメソッドどうやって使うんだっけかや❓❓
そうだ、google先生にきいてみようってなったとき
あ、いけねオフラインだった😱 orz.
そんな時に便利なRubyのdocがみれるコマンドを知ったので、簡単に紹介してみる。

Rubyが入っている環境で、riコマンドをいれるとコマンドラインから直接ドキュメントを確認できる★

$ ri
Please provide a class, module, or method name (e.g: ri Array#push)
# 引数に知りたいクラスや、モジュール、メソッド名を入れるとの事。

引数例を入れてみる

$ ri Array#push
Array#push

(from ruby core)
------------------------------------------------------------------------------
  ary.push(obj, ... )   -> ary

------------------------------------------------------------------------------

Append --- Pushes the given object(s) on to the end of this array. This
expression returns the array itself, so several appends may be chained
together. See also Array#pop for the opposite effect.

  a = [ "a", "b", "c" ]
  a.push("d", "e", "f")
          #=> ["a", "b", "c", "d", "e", "f"]
  [1, 2, 3,].push(4).push(5)
          #=> [1, 2, 3, 4, 5]

おお〜。Arrayクラスのpushメソッドのドキュメントが表示された🎉
See also Array#pop for the opposite effect.との事なので、そっちも確認してみる。

$ ri Array#pop
Array#pop

(from ruby core)
------------------------------------------------------------------------------
  ary.pop    -> obj or nil
  ary.pop(n) -> new_ary

------------------------------------------------------------------------------

Removes the last element from self and returns it, or nil if the
array is empty.

If a number n is given, returns an array of the last n elements
(or less) just like array.slice!(-n, n) does. See also Array#push for
the opposite effect.

  a = [ "a", "b", "c", "d" ]
  a.pop     #=> "d"
  a.pop(2)  #=> ["b", "c"]
  a         #=> ["a"]

それぞれappendするメソッドとremoveするメソッドとして説明されて、実際の使い方とサンプルがあってとてもわかり易い!英語の勉強にもなるね👍

うろ覚え検索対応

# 例)あの文字列操作の、チョなんとかって、なんだったっけ、、
$ ri String#cho
String#cho not found, maybe you meant:

String#chomp
String#chomp!
String#chop
String#chop!    👈 曖昧でもいくつか候補を出してくれる。シンセツカシコイ。

クラスそのものを入力すると継承関係が図解されてRubyが俯瞰できたりする。ちと壮大。

ri Class
Class < Module

(from ruby core)
------------------------------------------------------------------------------

Extends any Class to include json_creatable? method.

Classes in Ruby are first-class objects---each is an instance of class
Class.

Typically, you create a new class by using:

  class Name
   # some code describing the class behavior
  end

When a new class is created, an object of type Class is initialized and
assigned to a global constant (Name in this case).

When Name.new is called to create a new object, the new method
in Class is run by default. This can be demonstrated by overriding
new in Class:

  class Class
    alias old_new new
    def new(*args)
      print "Creating a new ", self.name, "\n"
      old_new(*args)
    end
  end

  class Name
  end

  n = Name.new

produces:

  Creating a new Name

Classes, modules, and objects are interrelated. In the diagram that follows,
the vertical arrows represent inheritance, and the parentheses metaclasses.
All metaclasses are instances of the class `Class'.
                           +---------+             +-...
                           |         |             |
           BasicObject-----|-->(BasicObject)-------|-...
               ^           |         ^             |
               |           |         |             |
            Object---------|----->(Object)---------|-...
               ^           |         ^             |
               |           |         |             |
               +-------+   |         +--------+    |
               |       |   |         |        |    |
               |    Module-|---------|--->(Module)-|-...
               |       ^   |         |        ^    |
               |       |   |         |        |    |
               |     Class-|---------|---->(Class)-|-...
               |       ^   |         |        ^    |
               |       +---+         |        +----+
               |                     |
  obj--->OtherClass---------->(OtherClass)-----------...
------------------------------------------------------------------------------
Class methods:

  new

Instance methods:

  allocate
  inherited
  json_creatable?
  new
  superclass

(from gem activesupport-5.1.2)
------------------------------------------------------------------------------

------------------------------------------------------------------------------
Instance methods:

  class_attribute
  descendants
  subclasses

------------------------------------------------------------------------------
Also found in:
  gem awesome_print-1.8.0

jupyternotebookでの活用🌟

スクリーンショット 2018-12-08 0.55.16.png

おわりに

riコマンドは、
- ビルドインや標準ライブラリは充実した内容を確認できる。
- オフラインでモクモクしててなにかちょっとサクッと調べるのに便利。
- あと英語の勉強になる。とりわけ英語のプログラミング用語に慣れるのにはよいかも〜。
- jupyter notebookと組み合わせてコードカキカキ✍しながら、その場で調べられるのは大変オススメ❗

うまくriコマンドを使ってたのしいRubyライフを送りましょう〜💎

6
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
6
2