[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Querying class attributes

John Doe

10/14/2004 6:34:00 PM


I'm creating a program that allows anonymous plugins to be defined. Since
the user can define their own plugins I want to check that they've defined
them properly. What I can't figure out is how to check the plugin's
correctness without instantiating an instance of it first thus making
checking impossible. I'm sure there is an easy way to do this but I don't
know what it is.

In the example below I want to check that the plugin has defined a
constructor that takes 4 parameters. I seem to only be able to check the
instance. How can I do it?

plugin = Class.new do
def initialize(a,b,c,d)
end
end
instance = plugin.new(0,0,0,0)

def test(object,name,method)
print name," responds to ",method.to_s
if object.respond_to?(method,true)
print " true\n"
print " arity ",object.method(method).arity,"\n"
else
print " false\n"
end
end

test(plugin,"plugin",:new)
test(plugin,"plugin",:initialize)
test(instance,"instance",:new)
test(instance,"instance",:initialize)


## resulting output

plugin responds to new true
arity -1
plugin responds to initialize true
arity -1
instance responds to new false
instance responds to initialize true
arity 4


36 Answers

Robert Klemme

10/15/2004 9:33:00 AM

0


"DaZoner" <nobody@nowhere.com> schrieb im Newsbeitrag
news:ckmgi5$r3k$1@news.doit.wisc.edu...
>
> I'm creating a program that allows anonymous plugins to be defined.
Since
> the user can define their own plugins I want to check that they've
defined
> them properly. What I can't figure out is how to check the plugin's
> correctness without instantiating an instance of it first thus making
> checking impossible. I'm sure there is an easy way to do this but I
don't
> know what it is.
>
> In the example below I want to check that the plugin has defined a
> constructor that takes 4 parameters. I seem to only be able to check the
> instance. How can I do it?

def test_class(cl)
raise "Wrong arity" unless cl.instance_method(:initialize).arity == 4
end

Kind regards

robert

>
> plugin = Class.new do
> def initialize(a,b,c,d)
> end
> end
> instance = plugin.new(0,0,0,0)
>
> def test(object,name,method)
> print name," responds to ",method.to_s
> if object.respond_to?(method,true)
> print " true\n"
> print " arity ",object.method(method).arity,"\n"
> else
> print " false\n"
> end
> end
>
> test(plugin,"plugin",:new)
> test(plugin,"plugin",:initialize)
> test(instance,"instance",:new)
> test(instance,"instance",:initialize)
>
>
> ## resulting output
>
> plugin responds to new true
> arity -1
> plugin responds to initialize true
> arity -1
> instance responds to new false
> instance responds to initialize true
> arity 4
>
>

John Doe

10/15/2004 10:39:00 PM

0

Much thanks - it works great.

A related question: Where is this documented? (I'm using the compiled help
that was bundled with version 1.8.x)


Robert Klemme

10/16/2004 9:04:00 AM

0


"DaZoner" <nobody@nowhere.com> schrieb im Newsbeitrag
news:ckpja5$g4u$1@news.doit.wisc.edu...
> Much thanks - it works great.
>
> A related question: Where is this documented? (I'm using the compiled help
> that was bundled with version 1.8.x)

I always use the online doc at rubydoc.org. Look for documentation of
classes Class and Module.

robert

Charles Hixson

10/17/2004 7:09:00 PM

0

Robert Klemme wrote:

> I always use the online doc at rubydoc.org. Look for documentation of
> classes Class and Module.
> robert

There's documentation there, allright, but the methods seem to be a bit
shy of definition...
I can't even tell if any of the databases will accept integer values
rather than string, much less object. And many of the methods don't
give any indication as to what the various parameters are supposed to
look like method(p1, p2) isn't very explicit. (I forget which method
that was. I was just checking out the site after your recommendation.)

That said, SOME of the methods seem reasonably, or even well,
documented. Presumably this is an ongoing project, but for now it's not
really suitable as a primary reference. Example code is needed to fill
in the gaps. Methods that don't occur in the examples are likely to
require digging into the source.





Robert Klemme

10/18/2004 7:27:00 AM

0


"Charles Hixson" <charleshixsn@earthlink.net> schrieb im Newsbeitrag
news:4172C21A.4050407@earthlink.net...
> Robert Klemme wrote:
>
> > I always use the online doc at rubydoc.org. Look for documentation of
> > classes Class and Module.
> > robert
>
> There's documentation there, allright, but the methods seem to be a bit
> shy of definition...
> I can't even tell if any of the databases will accept integer values
> rather than string, much less object. And many of the methods don't
> give any indication as to what the various parameters are supposed to
> look like method(p1, p2) isn't very explicit. (I forget which method
> that was. I was just checking out the site after your recommendation.)
>
> That said, SOME of the methods seem reasonably, or even well,
> documented. Presumably this is an ongoing project, but for now it's not
> really suitable as a primary reference. Example code is needed to fill
> in the gaps. Methods that don't occur in the examples are likely to
> require digging into the source.

.... or resort to some other form of doc, maybe Pickaxe II.

I find this doc quite comprehensive:
http://www.ruby-doc.org/core/classes/Module.ht...
http://www.ruby-doc.org/core/classes/UnboundM...

Although I'd readily admit that the path from Class to Module is not very
apparent since the line "Parent Module" is quite small and can be easily
overseen.

Btw, I use the Ruby Sidebar which is quite useful:
http://www.ruby-doc.org/docbar/to...

Kind regards

robert

James Britt

10/18/2004 8:04:00 AM

0

Robert Klemme wrote:
>
> "DaZoner" <nobody@nowhere.com> schrieb im Newsbeitrag
> news:ckpja5$g4u$1@news.doit.wisc.edu...
>
>> Much thanks - it works great.
>>
>> A related question: Where is this documented? (I'm using the compiled
>> help
>> that was bundled with version 1.8.x)
>
>
> I always use the online doc at rubydoc.org. Look for documentation of
> classes Class and Module.


Do you mean ruby-doc.org?


There are two sites with similar names:
ruby-doc.org (name has a dash in it), which is the Ruby
documentation project Web site
rubydoc.org, (no dash), which I believe was once a site for an online
book but has now been essentially abandoned.



James


Robert Klemme

10/18/2004 9:48:00 AM

0


"James Britt" <jamesUNDERBARb@neurogami.com> schrieb im Newsbeitrag
news:4173F795.80804@neurogami.com...
> Robert Klemme wrote:
> >
> > "DaZoner" <nobody@nowhere.com> schrieb im Newsbeitrag
> > news:ckpja5$g4u$1@news.doit.wisc.edu...
> >
> >> Much thanks - it works great.
> >>
> >> A related question: Where is this documented? (I'm using the compiled
> >> help
> >> that was bundled with version 1.8.x)
> >
> >
> > I always use the online doc at rubydoc.org. Look for documentation of
> > classes Class and Module.
>
>
> Do you mean ruby-doc.org?

Yes exactly. Sorry for the confusion.

robert

>
> There are two sites with similar names:
> ruby-doc.org (name has a dash in it), which is the Ruby
> documentation project Web site
> rubydoc.org, (no dash), which I believe was once a site for an online
> book but has now been essentially abandoned.
>
>
>
> James
>
>

Charles Hixson

10/18/2004 8:03:00 PM

0

Robert Klemme wrote:

>"Charles Hixson" <charleshixsn@earthlink.net> schrieb im Newsbeitrag
>news:4172C21A.4050407@earthlink.net...
>
>
>>Robert Klemme wrote:
>>
>>
>>
>>>I always use the online doc at rubydoc.org. Look for documentation of
>>>classes Class and Module.
>>> robert
>>>
>>>
>>There's documentation there, allright, but the methods seem to be a bit
>>shy of definition...
>>I can't even tell if any of the databases will accept integer values
>>rather than string, much less object. And many of the methods don't
>>give any indication as to what the various parameters are supposed to
>>look like method(p1, p2) isn't very explicit. (I forget which method
>>that was. I was just checking out the site after your recommendation.)
>>
>>That said, SOME of the methods seem reasonably, or even well,
>>documented. Presumably this is an ongoing project, but for now it's not
>>really suitable as a primary reference. Example code is needed to fill
>>in the gaps. Methods that don't occur in the examples are likely to
>>require digging into the source.
>>
>>
>
>.... or resort to some other form of doc, maybe Pickaxe II.
>
>
Pickaxe II has the same problem. In fact, they explicitly acknowledge
the problem towards the start of the book. Something about adding
proper documentation would have doubled the length of the book. (I
don't have it right to hand at the moment.) Their comment is that one
should use the on-line documentation, which has been improving rapidly,
but not as fast as the class libraries have been growing.

A part of the problem is that in a non-typed language just knowing how
many parameters there are doesn't tell one as much as it does in a typed
language. In C at least the automatic documentation can tell you
whether it's an integer or a string that's expected. Still, that's only
a small part of the problem, I've seen C documentation that's just as
bad or worse. The problem is that to effectively use a method you need
to know what how the parameters are interpreted, and no automatic
documentation system can do more than help with that. Even Eiffel,
which has some of the best automated documentation that I've seen, and
is strongly typed to boot, runs into that problem.

Ideally the programmer would document the code as he wrote it. (I know
how hard that is, but it does produce the best results.) A secondary
possibility is that someone else could go back through the code and
annotate it. This works, but takes longer, albeit the time isn't all
spent by the same person, and it can be done while debugging. Still,
that will never provide complete coverage.

>I find this doc quite comprehensive:
>http://www.ruby-doc.org/core/classes/Module.ht...
>http://www.ruby-doc.org/core/classes/UnboundM...
>
>Although I'd readily admit that the path from Class to Module is not very
>apparent since the line "Parent Module" is quite small and can be easily
>overseen.
>
>Btw, I use the Ruby Sidebar which is quite useful:
>http://www.ruby-doc.org/docbar/to...
>
>Kind regards
>
> robert
>
And I agree with you. Those two were quite well documented. (Well, I
didn't actually READ the documentation, just looked it over.) But for
many of the methods (not THOSE methods, but look, e.g., at EOFError) it
seems to be a bare list of methods & parameters, depending on the names
chosen for the parameters to carry the meaning. As if rdoc had been run
on uncommented code. (Which I know well, because my code usually starts
out that way.)

Still, the place I normally end up looking is in the standard library,
where I find things like <a
href="http://www.ruby-doc.org/stdlib/libdoc/sdbm/rdoc/index.html">sdbm....
NowI know that that is automatically generated, but that happened to be
the part I needed. (Past tense. I gave up and did things a different
way. But I still don't know if I *COULD* have used sdbm. I suspect
not, but I don't KNOW.)



Robert Klemme

10/19/2004 8:25:00 AM

0


"Charles Hixson" <charleshixsn@earthlink.net> schrieb im Newsbeitrag
news:4174203E.7010704@earthlink.net...
> Robert Klemme wrote:
>
> >"Charles Hixson" <charleshixsn@earthlink.net> schrieb im Newsbeitrag
> >news:4172C21A.4050407@earthlink.net...
> >
> >
> >>Robert Klemme wrote:
> >>
> >>
> >>
> >>>I always use the online doc at rubydoc.org. Look for documentation
of
> >>>classes Class and Module.
> >>> robert
> >>>
> >>>
> >>There's documentation there, allright, but the methods seem to be a
bit
> >>shy of definition...
> >>I can't even tell if any of the databases will accept integer values
> >>rather than string, much less object. And many of the methods don't
> >>give any indication as to what the various parameters are supposed to
> >>look like method(p1, p2) isn't very explicit. (I forget which method
> >>that was. I was just checking out the site after your
recommendation.)
> >>
> >>That said, SOME of the methods seem reasonably, or even well,
> >>documented. Presumably this is an ongoing project, but for now it's
not
> >>really suitable as a primary reference. Example code is needed to
fill
> >>in the gaps. Methods that don't occur in the examples are likely to
> >>require digging into the source.
> >>
> >>
> >
> >.... or resort to some other form of doc, maybe Pickaxe II.
> >
> >
> Pickaxe II has the same problem. In fact, they explicitly acknowledge
> the problem towards the start of the book. Something about adding
> proper documentation would have doubled the length of the book. (I
> don't have it right to hand at the moment.) Their comment is that one
> should use the on-line documentation, which has been improving rapidly,
> but not as fast as the class libraries have been growing.
>
> A part of the problem is that in a non-typed language just knowing how
> many parameters there are doesn't tell one as much as it does in a typed
> language. In C at least the automatic documentation can tell you
> whether it's an integer or a string that's expected. Still, that's only
> a small part of the problem, I've seen C documentation that's just as
> bad or worse. The problem is that to effectively use a method you need
> to know what how the parameters are interpreted, and no automatic
> documentation system can do more than help with that. Even Eiffel,
> which has some of the best automated documentation that I've seen, and
> is strongly typed to boot, runs into that problem.
>
> Ideally the programmer would document the code as he wrote it. (I know
> how hard that is, but it does produce the best results.) A secondary
> possibility is that someone else could go back through the code and
> annotate it. This works, but takes longer, albeit the time isn't all
> spent by the same person, and it can be done while debugging. Still,
> that will never provide complete coverage.
>
> >I find this doc quite comprehensive:
> >http://www.ruby-doc.org/core/classes/Module.ht...
> >http://www.ruby-doc.org/core/classes/UnboundM...
> >
> >Although I'd readily admit that the path from Class to Module is not
very
> >apparent since the line "Parent Module" is quite small and can be
easily
> >overseen.
> >
> >Btw, I use the Ruby Sidebar which is quite useful:
> >http://www.ruby-doc.org/docbar/to...
> >
> >Kind regards
> >
> > robert
> >
> And I agree with you. Those two were quite well documented. (Well, I
> didn't actually READ the documentation, just looked it over.) But for
> many of the methods (not THOSE methods, but look, e.g., at EOFError) it
> seems to be a bare list of methods & parameters, depending on the names
> chosen for the parameters to carry the meaning. As if rdoc had been run
> on uncommented code. (Which I know well, because my code usually starts
> out that way.)
>
> Still, the place I normally end up looking is in the standard library,
> where I find things like <a
>
href="http://www.ruby-doc.org/stdlib/libdoc/sdbm/rdoc/index.html">sdbm...
.
> NowI know that that is automatically generated, but that happened to be
> the part I needed. (Past tense. I gave up and did things a different
> way. But I still don't know if I *COULD* have used sdbm. I suspect
> not, but I don't KNOW.)

That's a really nice class! ;-)

I totally agree with you: unfortunately most developers have a much
stronger focus on getting the code right and working bug free than on
documentation. I know, often it's time pressure imposed by project
deadlines or whatever. But with open source that should be different.
And a crucial part of writing a library is getting the docs right. A lib
without proper doc will simply be ignored - and I guess nobody does write
code for the waste bin.

Personally I trie to document as I write, because it's much faster than
digging into the code later and it also helps me, if I have to go back
there after some time. After all, I have to maintain code and find bugs -
so I appreciate comments very much. Also, with Eclipse writing Javadoc is
just sooo simple.

Kind regards

robert




Adam Tomaszewski

6/20/2010 6:50:00 AM

0

On 19 Cze, 13:26, Piotr <petre...@gmail.com> wrote:
> On Jun 19, 5:04 am, Adam Tomaszewski <atomaszew...@sbcglobal.net>
> wrote:
>
>
>
> > On 19 Cze, 00:18, Piotr <petre...@gmail.com> wrote:
>
> > > On Jun 17, 10:22 am, Adam Tomaszewski <atomaszew...@sbcglobal.net>
> > > wrote:
>
> > > > On 17 Cze, 03:41, quark <quark...@yahoo.com> wrote:
>
> > > > > On Jun 17, 12:00 am, wojtek <cide...@yahoo.com> wrote:
>
> > > > > > Piotr wrote:
> > > > > > > On Jun 16, 8:25 pm, wojtek<cide...@yahoo.com>  wrote:
> > > > > > >> Piotr wrote:
> > > > > > >>> Cos sie Panu pomieszalo - Panskie posty moglby mnie
> > > > > > >>> denerwowac gdyby byl Pan czlowiekiem ktorego poglady i etyke
> > > > > > >>> szanuje. Lepszym wiec opisem mojej reakcji na Panskie posty
> > > > > > >>> jest zazenowanie i smutek ze az tak sie Pan stoczyl - na poczatku
> > > > > > >>> swego  pobytu na scp nie byl Pan tym samym czlowiekiem
> > > > > > >>> ktorym jest Pan  teraz i watpie by wtedy znizalby sie Pan do
> > > > > > >>> etyki p. Malca, trywializujac cierpienia ofiar masowych zbrodni
> > > > > > >>> okrutnych totalitarnych systemow dla doranzych przewag retorycznych
> > > > > > >>> i/lub podbudowania swojego ego:
>
> > > > > > >> Tepy rybi doktorek podobnie jak drugi tepak, niejaki Freier
> > > > > > >> ubrdali sobie w swoich tepych glowkach, ze sciepa to "mikrokosmos"
> > > > > > >> (czy jakos tak) i czerpia z niej pelnymi garsciami informacje
> > > > > > >> o jej czlonkach.
> > > > > > >> Ten drugi to przynajmniej wiadomo po co to robi, bo pracuje
> > > > > > >> w tej samej firmie co Zalek.
> > > > > > >> A piervyj?
> > > > > > >> Huj ego znajet shto w golowie imiejet takoj urod.
> > > > > > > [...]
>
> > > > > > > Klamie Pan, panie Krasucki. Mikrokosmos w argumentach
> > > > > > > p. Freiera nie mowil o pojedynczych  "czlonkach" a o spolecznosciach
> > > > > > > - wg p. Freiera Jedwabne bylo mikrokosmosem Polski: to co sie stalo w
> > > > > > > Jedwabnem mialo byc reprezentatywna ilustracja stosunku Polakow
> > > > > > > do Zydow w calej Polsce. Stad tez twierdzenia p. Freiera ze
> > > > > > > "wiekszosc" Polakow "nie byla ponadto" aby wydawac Zydow
> > > > > > > na smierc. Patrz:
> > > > > > >http://groups.google.ca/group/soc.culture.polish/msg/bff894......
> > > > > > > --
>
> > > > > > > Ja krytykowalem te podle generalizacje p. Freiera, np.
> > > > > > >http://groups.google.ca/group/soc.culture.polish/msg/750e26......
>
> > > > > > > I Pan o tym doskonale wie, bo uczestniczyl w tym samym
> > > > > > > watku, wie, A MIMO TO stawia znak  rownosci miedzy mna i podlymi
> > > > > > > generalizacjami  etnicznymi p. Freiera, ktore ja krytykowalem
>
> > > > > > > Czy trzeba  lepszego  dowodu na to jak nisko sie Pan
> > > > > > > stoczyl moralnie ?
>
> > > > > > > Panowie i Panie, pan Wojciech Krasucki.
>
> > > > > > > Piotr
>
> > > > > > Trela, ty jestes ciezko chory facet.
> > > > > > Ty nie masz zupelnie polotu, zdolnosci
> > > > > > do abstrakcyjnego myslenia, reagujesz
> > > > > > jak komputer z okreslona iloscia danych.
> > > > > > I ty i Freier wystawiacie laurki ludziom
> > > > > > na podstawie tego, co pisza na sciepie,
> > > > > > traktujecie sciepe jak "mikrokosmos".
> > > > > > Slowo <mikrokosmos> zapamietalem i prawidlowo
> > > > > > polaczylem z nazwiskiem wynalazcy - Freiera.
> > > > > > I te wrzaski - "Klamie Pan..." - jak opetany.
> > > > > > Wez ty przeczytaj moj post jeszcze raz, ale
> > > > > > uprzednio walnij sie w tepy leb solidnie.
> > > > > > Sloweczko "mikrokosmos" Freier uzywal takze
> > > > > > do sciepy i z zamilowaniem wystawial swiadectwa
> > > > > > moralnosci i poziomu inteligencji czlonkom tego
> > > > > > forum.
> > > > > > Ty robisz to samo.
> > > > > > I ten napuszony, impertynecki zwrot na koncu
> > > > > > kazdego twojego postu - "Panowie i Panie, pan itd."
> > > > > > Rzygac sie chce od twojej inteligencji tluku.
> > > > > > A ostatnio - falszerzu.
> > > > > >                                wk- Hide quoted text -
>
> > > > > > - Show quoted text -
>
> > > > > Trela swietnie by sie nadawal do pracy w rzadzie -- na jakimkolwiek
> > > > > poziomie, ale tam gdzie nalezy napisac uzasadnienie do jakiejs
> > > > > idiotycznej ustawy.
> > > > > Uzasadnienie musi byc tak dlugie i zawiklane aby czytajacemu
> > > > > odechcialo sie to czytac w pewnym momencie -- co tylko ratuje rzad
> > > > > przed nieszczesliwymi dla rzadu pytaniamai ze strony ofiar - wyborcow.
>
> > > > > DoTrelidotad nie dotarlo, ze nikt nie czyta tekstu, ktory sie wije
> > > > > jak soliter przez strony Internetowe.
> > > > > Nikt rowniez nie traktuje dowoduTrelipowaznie jesli to jest 'kupa'
> > > > > zdan wprowadzajacych i wyprowadzajacych i znow wprowadzajacych, bo
> > > > > tego nikt nie ma cierpliwosci by czytac.
>
> > > > > Moze to pisanie spelnia uTrelirole jaka przebieranie rozanca
> > > > > spelnialo u naszych babc. I moze to u niego dziala.
>
> > > > > s.
>
> > > > najlepiej usmialem sie jak p.Trela uzasadnial ze imiona Icka i Moska
> > > > sa  iminonami zakazanymi  bo jakis idiota wpisaj je do PWN jako imiona
> > > > obrazliwe,
>
> > > No wlasnie, gdzie tym idiotom do scepowego Wielkiego
> > > Jezykoznawcy. Idioci z PWN  nie wiedza ze pisanie o
> > >  "moskach smierdzacych czosnkiem i cebula" to tak
> > > naprawde  wyraz Panskiego glebokiego szacunku i
> > > szczerej sympatii. ktore Pan zywi dla Zydow i ich
> > > kultury. Tfu,  co za zakute paly.
>
> > > Tylko patrzec az zaczna nam wmawiac, ze smianie sie
> > > z Czechow jako "pepikow" to  wcale nie calkowicie neutralne
> > > nawiazanie do czeskiego imienia , lecz, prosze sobie
> > > wyobrazic, lekcewazace  (hahaha haha!) okreslenie Czechow !
>
> > > Albo beda nam tlumaczyc ze pisanie  "polaczki" jest
> > > obrazliwe ;-))))))))),  podcza gdy nawet dziecko  wie ze, to
> > > tylko takie mile zdrobnienie  slowa "Polacy".
>
> > > Piotr
>
> > > P.S. Doceniam odwage Panskiej szczerosci wyjscia z klozetu
> > > ze swoja sympatia do Zydow, ale czy nie boi sie Pan
> > > ze skoro oficjalnie zamanifestowal ze Panskie uczucia
> > > wobec Zydow nie sa wcale negatywne, to boukun i
> > > Pan Slawista uznaja Pana za sprzedawczyka?
>
> > Trela wam calkiem bije pod dekiel1  gdzie za pisalem ze zydzi to moski
> > smierdzace cebula i czosnkiem??, nogi z dupy powyrywam jak staniesz na
> > mej drodze bo dopuszczacie sie oszczerstwa niewybaczalnego,
>
> akurat to twierdzenie mozna spradzic faktami: mowi Pan prawde to
> nie bedzie Pan mial problemu  przypomniec nazwiska  wszystkich tych
> ludzi ktory nazwal Pan "moskami  smierdzacymi czosnkiem  i cebula"
> i ktorych jednoczesnie NIE uwaza Pan za Zydow
> lub ich sympatykow.
>
> Smialo, panie Tomaszewski, nie robil Pan chyba sobie z geby
> cholewy?
>
> Piotr

ales pan niezmiernie glupi i w dodatku bajkopisarz, kazda historie
dopasuje sobie p. do swoich potrzeb, jeszcze raz ,
prosze wskazac kiedy i gdzie napisalem ze zydzi to moski cuchnace
cebula i czosnkiem?