query: tag:irb

ActiveRecordを読み込んだ状態でirbを使っているときに、TAB補完をしようとすると落ちることが結構な頻度でありました。
原因を調べてみると、補完候補のArrayをsort使用としているところで、Symbolに対して <=> 演算子を呼びだそうとしてエラーが発生しているようです。
補完候補は Object#instance_methods から得ているようなので、なぜSymbolが要素として混入するのかが謎だったのですが、どうやら
Arel::Attribute::Predications クラスが犯人のようです。

ruby>>
PREDICATES = [
:eq, :eq_any, :eq_all, :not_eq, :not_eq_any, :not_eq_all, :lt, :lt_any,
:lt_all, :lteq, :lteq_any, :lteq_all, :gt, :gt_any, :gt_all, :gteq,
:gteq_any, :gteq_all, :matches, :matches_any, :matches_all, :not_matches,
:not_matches_any, :not_matches_all, :in, :in_any, :in_all, :not_in,
:not_in_any, :not_in_all
]

Predications = Class.new do
def self.instance_methods *args
warn "this module is deprecated, please use the PREDICATES constant"
PREDICATES
end
end
<<--

このコードを書いた人は instance_methodsはStringの配列を返すという事を失念していたようですね。
irb/completeはObjectSpaceから補完候補を拾ってくるので、上述のコードがあるせいでSymbolが補完候補配列に混入してしまいます。

回避策としては、irb起動時に以下のようなコードを読み込むなどがあります。

ruby>>
module Arel
class Attribute
class Predications
def self.instance_methods
require 'arel/algebra/attributes/attribute'
Arel::Attribute::PREDICATES.map(&:to_s)
end
end
end
end
<<--

追記

この問題は 1.8.x系のRubyでのみ発生するようです。
thanks
@a_matsuda!

posted by genki genki on Thu 7 Jul 2011 at 12:31 with 0 comments

I added a new feature to irb_rocket.
It's a simple utility for ease of daily irb use.
Have you ever felt annoying for typing left cursor to enclose exprs by parens to be receiver like this?

ruby>>
irb> 1 + 2 + 3 # Oh, I want to call "to_s"
irb> (1 + 2 + 3 # back to the head
irb> (1 + 2 + 3).to_s # return to the tail
<<--

I've experienced stuffs like this very often.
Very annoying.

So I added the new feature named "irb_dollar"
Its usage is simple, like this.

ruby>>
irb> require "irb_dollar"
irb> 1 + 2 + 3 $ to_s #=> "6"
<<--

Nice!

You can use "$" sign to make the left hand statement be receiver by installing irb_rocket version that is newer than 0.2.0

Enjoy :)

Links

posted by genki genki on Mon 3 Jan 2011 at 18:56 with 0 comments

Needless to say, we can't live without irb.
Although it is already incredibly useful,
I had a desire to improve it...

like this:

ss

This is the
irb_rocket
that I've made.
It makes irb into being as follows

  • Result follows blue #=> after your input.
  • It also be colored by Wirble.
  • Output of stderr is colored to red.

INSTALL

You must install
Wirble
and
ruby-terminfo
in advance.
And then, you can install the plugin like this:

pre>>
% sudo gem install irb_rocket --source http://merbi.st
<<--

After that, the last task is append 2 lines below to your ~/.irbrc

ruby>>
require 'rubygems'
require 'irb_rocket'
<<--

Completed!
Now you can use new irb as always.

posted by takiuchi takiuchi on Sun 8 Feb 2009 at 17:15 with 4 comments

irbはインタラクティブにrubyのコードを実行出来る
非常に便利なユーティリティですが、
脳内にある「irbのあるべき姿」と比べると、
ちょっとだけ足りないところがあると思っていました。

そこで、思い通りの動きをするようにirbをカスタマイズする
irbプラグイン
irb_rocket
を作ってみました。
これを使うと、いつも見慣れたirbが、こんな風になります。

ss

  • inputの末尾に#=>に続いて結果が表示されます
  • 結果はWirbleを使って色づけしています
  • 標準エラー出力への出力は赤で表示します

インストール方法

irb_rocketは
wirble

ruby-terminfo
に依存しているので、
それらをインストールします。
続いて、以下のようにhttp://merbi.stからgemをインストールします。

pre>>
% sudo gem install irb_rocket --source http://merbi.st
<<--

最後に、~/.irbrcに、以下のコードを加えます。

ruby>>
require 'rubygems'
require 'irb_rocket'
<<--

あとは、いつものようにirbを起動するだけです。

追記

ruby-terminfoはakrさん作のterminfoのruby用バインディングです。
インストールが大変だったので、gem化してこちらに置いておきました。

以下のようにインストール出来ます。
pre>>
% sudo gem install ruby-terminfo --source http://merbi.st
<<--

posted by genki genki on Sun 8 Feb 2009 at 16:53 with 4 comments

irbを使っているときに、オブジェクトにどんなメソッドが
定義されているのかをpublic_methodsなどで調べることが
出来ますが、たいていの場合大量のメソッドがリストアップされて
わかりにくくなってしまいます。

今回は、そんなときに良くやる方法を紹介します。

ruby>>

Time.public_methods.sort - Object.public_methods
=> ["_load", "at", "days_in_month", "gm", "httpdate", "iso8601", "local", "local_time", "mktime", "now", "parse", "rfc2822", "rfc822", "time_with_datetime_fallback", "times", "today", "utc", "utc_time", "xmlschema", "yaml_new", "zone_offset"]
<<--

public_methodsからObjectpublic_methodsを取り除いたものを
表示しています。Objectの代わりに適当な親クラスを指定することで、
クラス階層の特定の領域で定義されたメソッドを表示することも
できますね。

posted by genki genki on Mon 21 Jan 2008 at 14:14 with 0 comments