[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Finding methods of instance itself

List Recv

7/19/2006 3:52:00 AM

I use IRB to explore. Very often I type a.methods.sort, to see what an
object can do. The problem is, you're hit with 100 methods from Object
(or the superclass).

I've experimented with:
a.methods - a.class.superclass.methods
which works well for simple things

but it doesn't seem to handle cases when there are a lot of included
modules. Any ideas on how to see the methods defined in a.class only,
and not in any superclass or included module?

5 Answers

Trans

7/19/2006 4:25:00 AM

0


listrecv@gmail.com wrote:
> I use IRB to explore. Very often I type a.methods.sort, to see what an
> object can do. The problem is, you're hit with 100 methods from Object
> (or the superclass).
>
> I've experimented with:
> a.methods - a.class.superclass.methods
> which works well for simple things
>
> but it doesn't seem to handle cases when there are a lot of included
> modules. Any ideas on how to see the methods defined in a.class only,
> and not in any superclass or included module?

methods has a switch:

object.methods(false)

and

klass.instance_methods(false)

Also, keep in mind the differences between:

methods
public_methods
private_methods
protected_methods

instance_methods
public_instance_methods
private_instance_methods
protected_instance_methods

I've made a fool of myself more than once thinking the first combined
the later three -- it does not. methods is alias of public_methods and
likewise for the instance version.

T.


List Recv

7/19/2006 4:40:00 AM

0


transfire@gmail.com wrote:
> methods has a switch:
>
> object.methods(false)
>
> and
>
> klass.instance_methods(false)

Thanks!

Pit Capitain

7/19/2006 7:43:00 AM

0

transfire@gmail.com schrieb:
> Also, keep in mind the differences between:
>
> methods
> public_methods
> private_methods
> protected_methods
>
> instance_methods
> public_instance_methods
> private_instance_methods
> protected_instance_methods
>
> I've made a fool of myself more than once thinking the first combined
> the later three -- it does not.

Me too.

> methods is alias of public_methods and
> likewise for the instance version.

That's not quite correct. Kernel#methods and Module#instance_methods
return both public and protected methods:

class C
public; def pub; end
protected; def prot; end
private; def priv; end
end

p C.instance_methods(false) # => ["prot", "pub"]

Regards,
Pit

Paul Battley

7/19/2006 8:11:00 AM

0

On 19/07/06, listrecv@gmail.com <listrecv@gmail.com> wrote:
> I use IRB to explore. Very often I type a.methods.sort, to see what an
> object can do. The problem is, you're hit with 100 methods from Object
> (or the superclass).
>
> I've experimented with:
> a.methods - a.class.superclass.methods
> which works well for simple things
>
> but it doesn't seem to handle cases when there are a lot of included
> modules. Any ideas on how to see the methods defined in a.class only,
> and not in any superclass or included module?

This isn't exactly the answer to your question, but I've defined a
couple of methods in my .irbrc that I've found really useful. The
first is methods aliased to methods.sort (because that's always what I
want in irb, and the overhead doesn't matter there). The second is
distinct_methods, which is all the methods that aren't inherited from
Object:

class Object
alias_method :__methods__, :methods
def methods
return __methods__.sort
end
def distinct_methods
(__methods__ - Object.__methods__).sort
end
end

The latter generally produces a manageable list, so I haven't found
the need to exclude methods from included modules or superclasses.

Paul.

Smiler

6/21/2009 12:35:00 AM

0

Ayatollah Dank CDXX wrote:
> On Jun 20, 1:38 pm, staten <state...@lycos.com> wrote:
>> Just 8 Years After 9/11 and NYC Schools are forced by the New York
>> City Council Education Committee to observe holidays of the barbaric
>> Muslim religion that created a huge crater in the downtown of
>> Manhattan. This is another example of absurdity and idiocy of
>> liberals and socialists - the more attacks there are, the more we are
>> asked to respect Islam. Just a few more years and memorials to the
>> victims of 9/11 are not allowed anymore in NYC, as it might offend
>> Muslims and their hatred. And why not? When a Muslim is in the White
>> House you can expect anything.
>
> This wouldn't necessarily be so bad except that public schools around
> the USA are forbidden from observing xian holidays such as xmas and
> easter, on the grounds that such recognition violates separation of
> church and state.

Public schools are OPEN at Christmas and Easter????
If not, why should they not also close for other religions' 'holy days'?
Or should they be open on ALL religious 'holy days', Easter and Christmas
included?
That would cause no problem for atheists.

>
> And notice how radical atheists who protest everything xian and demand
> the strictest separation of church and state are unusually silent
> about the trend of celebrating izlamist holidays in public schools.

No 'celebrations' are allowed, just closure.

--
Smiler,
The godless one
a.a.# 2279
All gods are bespoke. They're all individually tailor
made to perfectly fit the prejudices of their believer.