--nextPart21245075.QKPKlLWXq2
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="us-ascii"

On Tuesday 03 June 2014 16:51:07, Roelof Wobben <r.wobben / home.nl> wrote:
> Sorry,. I read the whole topic again but I cannot find anything about
> .instance_methods or do you mean .lookup or ,responds_to.
> 
> Then I still not clear to me what I schould do when I see
> SantaClaus(SantaClaus.new)
> Could I do SantaClaus.lookup SantaClaus.new  ?

AFAIR, what you want to archieve is to check whether an Object responds to a 
number of methods, i.e., "implements an interface". (Please not the quotation 
marks, I'm still not happy with that notion.)

So, suppose your "interface" consists of the methods "method_a", "method_b" 
and "method_c", you check whether a given object responds to all three.

The first, simple version can then look like this:

````
def test_responds_to_my_methods
	my_obj = MyClass.new
	responds_to_all = true

	[ :method_a, :method_b ].each do |method|
		responds_to_all &= my_obj.respond_to? method
	end
end
````

The method .instance_methods returns an Array of Symbols, each Symbol being 
one method the object responds to.

You can also pass "false" as parameter to .instance_methods in order to only 
include the methods the class itself defines. This allows you even better 
"interface checking".

Consider:


````
class MyPseudoInterface
	def method_a
	end

	def method_b
	end
end


MyPseudoInterface.instance_methods(false) # => [:method_a, :method_b]
`````


If you've got an object, you need to gain access to the class before you can 
call .instance_methods. You do that using "my_obj.class", i.e., 
"my_obj.class.instance_methods".

(Notice the dot instead of the hash in the text: An instance method is 
typically written using an hash as separator, e.g., 
MyClass#an_instance_method. Class methods are separated using a dot.)

Putting it all together, you can solve your exercise.

HTH.

			--- Eric
--nextPart21245075.QKPKlLWXq2
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: This is a digitally signed message part.
Content-Transfer-Encoding: 7Bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAABAgAGBQJTjeZ0AAoJEIXD2TZ1epVBh/IP/06xQS53ciDZqvla/oaZ9TFH
77c4ePKJqKrsGY1w3ZNh9rxZrxmZB0VrCikOXSzqrPQWHLU1iujtHtRoROsMo4CL
uNHGmsDMS62lsobFAKEkAQS1RbN6+tNBjui8Ra3H1kIYwLYbOVTNxmiUgih1nPRM
EgM30Kp1W7yV6bxki5e3zmNC0pMS78so8jdiUByrSQ5aj83C36A9d3zr0roPArKz
9RFwoup9TyUuOVAVWuzuPtpGWi3tg3U2qJ28tW+jpuZRQl/F4JYzp8dNr4GZtMQq
jxWfL0CdLMowLKo/PA1b7GpBhX4/SVZyT64PhNCWzxvCi3T6awpQL3IP+m0IjjVw
zaQebI9DvxBN/wuCoPoSbx/W0P83fS7eeui5kR1W+rWq1bgEvMyeoH13gIBM9CTB
F77tOS4ix/mmIvtx0oaw/8ORMc1Cl5mxLGcEJBJlo6TkfvPdUFxQ4emwFXt4cUOf
bvb7rVEl9or9QADXzpEbAZDG3ijtxw5MOc/bh+jyyljfCAws6hEvcfJ9tQrGrEHg
JhH9XjX14rGfh/SiCcSplXB2VmMtkh1pENU1jh2MEJNZt73dzSevrEUkkkt0kOr2
ijk+Lgx/FMwI3HqFOH5JvFCdgY/zvXeMxoV1mpihnEL1Swg9ieM2B+ePiXShDAaW
R3XwZ4wyxdNMcr/LswUR
=x6J6
-----END PGP SIGNATURE-----

--nextPart21245075.QKPKlLWXq2--