12

nilに似た論理式でfalseと評価されるオブジェクトをRubyで作成するにはどうすればよいですか?

私の意図は、チェーンの途中で値が通常 になる他のオブジェクトのネストされた呼び出しを有効にすることですnilが、すべての呼び出しを続行して、それ自体ではなく nil のようなオブジェクトを返すことnilです。オブジェクトは、処理方法がわからない受信メッセージに応答して自分自身を返しますnil?。.

例えば:

fizz.buzz.foo.bar

buzzプロパティが利用できない場合は、nil のようなオブジェクトを返します。このオブジェクトは、それ自体fizzを返すまで呼び出しを受け入れます。bar最終的に、上記のステートメントは false と評価されるはずです。

編集:

以下のすべての素晴らしい回答に基づいて、次のことを思いつきました。

class NilClass
  attr_accessor :forgiving
  def method_missing(name, *args, &block)
    return self if @forgiving
    super
  end
  def forgive
    @forgiving = true
    yield if block_given?
    @forgiving = false
  end
end

これにより、次のような卑劣なトリックが可能になります。

nil.forgiving {
    hash = {}
    value = hash[:key].i.dont.care.that.you.dont.exist
    if value.nil?
        # great, we found out without checking all its parents too
    else
        # got the value without checking its parents, yaldi
    end
}

明らかに、関数呼び出し/クラス/モジュール/どこでも、このブロックを透過的にラップできます。

4

4 に答える 4

9

これは、問題に取り組む方法のアイデアとコードサンプルの束を含むかなり長い答えです。

try

Railsには、そのようにプログラムできるtryメソッドがあります。これは、その実装方法の一種です。

class Object
  def try(*args, &b)
    __send__(*a, &b)
  end
end

class NilClass        # NilClass is the class of the nil singleton object
  def try(*args)
    nil
  end
end

次のようにプログラムできます。

fizz.try(:buzz).try(:foo).try(:bar)

よりエレガントなAPIをサポートするために、これを少し異なる方法で動作するように変更することもできます。

class Object
  def try(*args)
    if args.length > 0
      method = args.shift         # get the first method
      __send__(method).try(*args) # Call `try` recursively on the result method
    else
      self                        # No more methods in chain return result
    end
  end
end
# And keep NilClass same as above

次に、次のことができます。

fizz.try(:buzz, :foo, :bar)

andand

そして、 NilClassサブクラスを直接インスタンス化できないという事実をハッキングして、より悪質な手法を使用します。

class Object
  def andand
    if self
      self
    else               # this branch is chosen if `self.nil? or self == false`
      Mock.new(self)   # might want to modify if you have useful methods on false
    end
  end
end

class Mock < BasicObject
  def initialize(me)
    super()
    @me = me
  end
  def method_missing(*args)  # if any method is called return the original object
    @me
  end
end

これにより、次のようにプログラムできます。

fizz.andand.buzz.andand.foo.andand.bar

いくつかの派手な書き直しと組み合わせる

ここでも、この手法を拡張できます。

class Object
  def method_missing(m, *args, &blk)        # `m` is the name of the method
    if m[0] == '_' and respond_to? m[1..-1] # if it starts with '_' and the object
      Mock.new(self.send(m[1..-1]))         # responds to the rest wrap it.
    else                                    # otherwise throw exception or use
      super                                 # object specific method_missing
    end
  end
end

class Mock < BasicObject
  def initialize(me)
    super()
    @me = me
  end
  def method_missing(m, *args, &blk)
    if m[-1] == '_'  # If method ends with '_'
      # If @me isn't nil call m without final '_' and return its result.
      # If @me is nil then return `nil`.
      @me.send(m[0...-1], *args, &blk) if @me 
    else 
      @me = @me.send(m, *args, &blk) if @me # Otherwise call method on `@me` and
      self                                  # store result then return mock.
    end
  end
end

何が起こっているのかを説明するために:下線付きのメソッドを呼び出すと、モックモードがトリガーされ、の結果は自動的にオブジェクト_methにラップされます。Mockそのモックでメソッドを呼び出すと、そのモックがanilを保持していないかどうかをチェックし、メソッドをそのオブジェクト(ここでは@me変数に格納されています)に転送します。次に、モックは元のオブジェクトを関数呼び出しの結果に置き換えます。呼び出すmeth_と、モックモードが終了し、の実際の戻り値が返されますmeth

これにより、次のようなAPIが可能になります(アンダースコアを使用しましたが、実際には何でも使用できます)。

fizz._buzz.foo.bum.yum.bar_

残忍なモンキーパッチアプローチ

これは本当に厄介ですが、エレガントなAPIを可能にし、アプリ全体のエラー報告を必ずしも台無しにするわけではありません。

class NilClass
  attr_accessor :complain
  def method_missing(*args)
    if @complain
      super
    else
      self
    end
  end
end
nil.complain = true

このように使用します:

nil.complain = false
fizz.buzz.foo.bar
nil.complain = true
于 2012-01-07T00:55:03.983 に答える
6

私の知る限り、これを行う簡単な方法はありません。あなたが話している機能を実装するいくつかの作業がRubyコミュニティで行われています。あなたは見てみたいかもしれません:

andandgemは次のように使用されます。

require 'andand'
...
fizz.buzz.andand.foo.andand.bar
于 2012-01-07T00:28:30.740 に答える
3

まだ定義されていないメソッドに応答するためNilClassに使用するクラスを変更できます。method_missing()

> class NilClass
>   def method_missing(name)
>     return self
>   end
> end
=> nil
> if nil:
*   puts "true"
> end
=> nil
> nil.foo.bar.baz
=> nil
于 2012-01-07T00:29:50.473 に答える
2

Demeter の法則 [1] と呼ばれる原則があります。これは、オブジェクトが必ずしも他のオブジェクトの関係について多くを知っている必要がないため、実行しようとしていることは良い習慣ではないことを示唆しています。

しかし、私たちは皆それをします:-)

単純なケースでは、属性の連鎖を存在をチェックするメソッドに委任する傾向があります。

class Fizz
  def buzz_foo_bar
    self.buzz.foo.bar if buzz && buzz.foo && buzz.foo.bar
  end
end

これで、例外が発生しないことを知って fizz.buzz_foo_bar を呼び出すことができます。

しかし、欠落しているメソッドを処理し、アンダースコアを検索し、反映された関連付けをテストして、それらがチェーンの残りの部分に応答するかどうかを確認するコードのスニペットも取得しました (作業中であり、来週まで取得できません)。これは、デリゲート メソッドなどを記述する必要がなくなったことを意味します。method_missing パッチを含めるだけです。

module ActiveRecord
  class Base
    def children_names
      association_names=self.class.reflect_on_all_associations.find_all{|x| x.instance_variable_get("@macro")==:belongs_to}
      association_names.map{|x| x.instance_variable_get("@name").to_s} | association_names.map{|x| x.instance_variable_get("@name").to_s.gsub(/^#{self.class.name.underscore}_/,'')}
    end

    def reflected_children_regex
      Regexp.new("^(" << children_names.join('|') << ")_(.*)")
    end

    def method_missing(method_id, *args, &block)
      begin
        super
      rescue NoMethodError, NameError
        if match_data=method_id.to_s.match(reflected_children_regex)
          association_name=self.methods.include?(match_data[1]) ? match_data[1] : "#{self.class.name.underscore}_#{match_data[1]}"
          if association=send(association_name)
            association.send(match_data[2],*args,&block)
          end
        else
          raise
        end
      end
    end
  end
end

[1] http://en.wikipedia.org/wiki/Law_of_Demeter

于 2012-01-07T01:38:21.060 に答える