2
0

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 5 years have passed since last update.

Object#methods が便利

Posted at

はじめに

Rubyにおいてオブジェクトに対して実行できるメソッドを知りたいときありませんか?そんなときに使える便利メソッドを紹介します。

Object#methods

Object#methods はそのオブジェクトに対して呼び出せるメソッド名の一覧を返します。

詳しい使い方はこちら
https://docs.ruby-lang.org/ja/latest/method/Object/i/methods.html

> 'mikaji'.methods.grep /to_/
[
    [ 0]      psych_to_yaml(*options) String (Object)
    [ 1]               to_c()         String
    [ 2]            to_enum(*arg1)    String (Kernel)
    [ 3]               to_f()         String
    [ 4]               to_i(*arg1)    String
    [ 5]               to_r()         String
    [ 6]               to_s()         String
    [ 7]             to_str()         String
    [ 8]             to_sym()         String
    [ 9]            to_yaml(*options) String (Object)
    [10] to_yaml_properties()         String (Object)
]

このようにRailsコンソールからの場合は元から定義されているメソッドだけでなく新たに定義されたメソッドもすべて表示されます。

おまけ

pryのlsも便利らしい

> ls 'mikaji'
Comparable#methods: <  <=  >  >=  between?  clamp
String#methods:
  %    ==           black       capitalize!  chop        crypt      dump            encode!         gray      hex       lines    next!         purple     rindex      scrub!       split        sub!       to_f    tr!                  unpack1          yellow
  *    ===          blue        casecmp      chop!       cyan       each_byte       encoding        grayish   include?  ljust    oct           purpleish  rjust       setbyte      squeeze      succ       to_i    tr_s                 upcase           yellowish
  +    =~           blueish     casecmp?     chr         cyanish    each_char       end_with?       green     index     lstrip   ord           red        rpartition  shellescape  squeeze!     succ!      to_r    tr_s!                upcase!
  +@   []           bytes       center       clear       delete     each_codepoint  eql?            greenish  insert    lstrip!  pale          redish     rstrip      shellsplit   start_with?  sum        to_s    unicode_normalize    upto
  -@   []=          bytesize    chars        codepoints  delete!    each_line       force_encoding  gsub      inspect   match    partition     replace    rstrip!     size         strip        swapcase   to_str  unicode_normalize!   valid_encoding?
  <<   ascii_only?  byteslice   chomp        concat      downcase   empty?          freeze          gsub!     intern    match?   prepend       reverse    scan        slice        strip!       swapcase!  to_sym  unicode_normalized?  white
  <=>  b            capitalize  chomp!       count       downcase!  encode          getbyte         hash      length    next     pretty_print  reverse!   scrub       slice!       sub          to_c       tr      unpack               whiteish

おわりに

さくっとメソッド知りたいときも使えるし、こんなメソッドあったんだという発見にもなるのでおすすめです。

2
0
7

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?