pry、pry-docを使ってRuby学習ヘ(^o^)ノ
Rubyでコードを書いているとメソッドの使い方をググったり、 ドキュメントみたりしますよねー
さらっと機能をご紹介♪
インストール
$ gem install pry pry-doc
起動
コマンドラインでpryコマンドを実行します。
$ pry [1] pry(main)>
スクリプトの実行
[1] pry(main)> puts "Hello, Pry!" Hello, Pry! => nil
オブジェクトへ移動
[2] pry(main)> cd "Hello, Pry!" [3] pry("Hello, Pry!"):1>
おおー、文字列オブジェクトの中へ移動しましたねー
メソッド一覧表示
[3] pry("Hello, Pry!"):1> ls Comparable#methods: < <= > >= between? String#methods: % capitalize delete force_encoding lstrip rstrip start_with? to_str * capitalize! delete! freeze lstrip! rstrip! strip to_sym + casecmp downcase getbyte match scan strip! tr << center downcase! gsub next scrub sub tr! <=> chars dump gsub! next! scrub! sub! tr_s == chomp each_byte hash oct setbyte succ tr_s! === chomp! each_char hex ord shell_split succ! unpack =~ chop each_codepoint include? partition shellescape sum upcase [] chop! each_line index prepend shellsplit swapcase upcase! []= chr empty? insert replace size swapcase! upto ascii_only? clear encode inspect reverse slice to_c valid_encoding? b codepoints encode! intern reverse! slice! to_f bytes concat encoding length rindex split to_i bytesize count end_with? lines rjust squeeze to_r byteslice crypt eql? ljust rpartition squeeze! to_s self.methods: __pry__ locals: _ __ _dir_ _ex_ _file_ _in_ _out_ _pry_
メソッド一覧を取得できましたー
メソッドのgrep
grepオプションで絞り込みましょう
[4] pry("Hello, Pry!"):1> ls --grep ! String#methods: capitalize! chop! downcase! gsub! next! rstrip! slice! strip! succ! tr! upcase! chomp! delete! encode! lstrip! reverse! scrub! squeeze! sub! swapcase! tr_s!
破壊的メソッドだけ表示できましたー
ヘルプを参照する
[5] pry("Hello, Pry!"):1> ri String#chop! String#chop! (from ruby core) ------------------------------------------------------------------------------ str.chop! -> str or nil ------------------------------------------------------------------------------ Processes str as for String#chop, returning str, or nil if str is the empty string. See also String#chomp!.
riコマンドはrdocの内容を表示します。
pry-docを使ってみましょうー
[6] pry("Hello, Pry!"):1> ? chop! From: string.c (C Method): Owner: String Visibility: public Signature: chop!() Number of lines: 3 Processes str as for String#chop, returning str, or nil if str is the empty string. See also String#chomp!.
? メソッドでヘルプを参照できます
[7] pry(main)> ? [].map From: array.c (C Method): Owner: Array Visibility: public Signature: map() Number of lines: 12 Invokes the given block once for each element of self. Creates a new array containing the values returned by the block. See also Enumerable#collect. If no block is given, an Enumerator is returned instead. a = [ "a", "b", "c", "d" ] a.collect { |x| x + "!" } #=> ["a!", "b!", "c!", "d!"] a.map.with_index{ |x, i| x * i } #=> ["", "b", "cc", "ddd"] a #=> ["a", "b", "c", "d"]
いいかんじですねー
Rubyのソースコードを見てみる
[8] pry(main)> $ [].map From: array.c (C Method): Owner: Array Visibility: public Number of lines: 13 static VALUE rb_ary_collect(VALUE ary) { long i; VALUE collect; RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length); collect = rb_ary_new2(RARRAY_LEN(ary)); for (i = 0; i < RARRAY_LEN(ary); i++) { rb_ary_push(collect, rb_yield(RARRAY_AREF(ary, i))); } return collect; }
どうですか?
これである程度はカバーできるはずですw
pryにはまだまだたくさんの機能が存在します。
shell-modeとか
詳しくはREADMEをみてねー
Happy Hacking٩( ‘ω’ )و