Imagine you're building a method which can have several types of object
passed in to it. It is logical to assume that you're Going to do roughly
the same thing with those objects. As such one checks what methods the
thing responds to rather than the type of the thing.

Sent from my phone please excuse my brevity.

On 9 Jul 2014, at 10:34, Arup Rakshit <[email protected]> wrote:

On Wed, Jul 9, 2014 at 2:15 PM, Sam Phippen <[email protected]>
wrote:

> Duck type means that it will respond to that method, instance of will
> check the class of the object.
>

Ah! Got it.

describe '#duck_type' do
  let(:dbl) { double() }
  before { allow(dbl).to receive(:random_method).with(duck_type(:size)) }

  context "when the argument is String" do
    it "should match duck type with one method" do
      dbl.random_method([])
    end
  end

  context "when the argument is Array" do
    it "should match duck type with one method" do
      dbl.random_method("")
    end
  end
end

Result :-

arup$ rspec test_spec.rb
..

Finished in 0.00094 seconds
2 examples, 0 failures

Good. But what's the use of to check of if any argument to respond to a
specific method or not. Is it indirect check to see if the argument object
belongs to a specific class or not ? I mean, I want my #foo method always
accept arguments as String object only, not any other. Is this the check
that duck_type helps us to perform ? Or Am I completely wrong, there is
some thing more interesting concept behind it ?


Regards,
A.R




 --
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/CAKxHnE0S6bJ7t11w-xCJLkX%3DosYGgpgdD1FF%2BL4MpfbyNsS%3D2Q%40mail.gmail.com
<https://groups.google.com/d/msgid/rspec/CAKxHnE0S6bJ7t11w-xCJLkX%3DosYGgpgdD1FF%2BL4MpfbyNsS%3D2Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/-3848092859628622356%40unknownmsgid.
For more options, visit https://groups.google.com/d/optout.

Reply via email to