[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

getting the names of classes defined in a ruby script

Thilina Buddhika

11/8/2007 4:53:00 AM

I want to get the names of the classes defined in a Ruby script. Is
there any possible way of doing this ?

thanks!

regs,
buddhika
--
Posted via http://www.ruby-....

4 Answers

Michael Linfield

11/8/2007 4:22:00 PM

0

Thilina Buddhika wrote:
> I want to get the names of the classes defined in a Ruby script. Is
> there any possible way of doing this ?
>
> thanks!
>
> regs,
> buddhika

Probably not the best way of doing it but...

file = File.readlines("rubyscript.rb")
res = []
file.each do |x|
res << x
end

# then using regular expressions you could filter out the upper case
words
# to be more accurate you could develop a gsub or iteration that
includes the
# word 'class'

i forget the reg. exp. for it but i believe its along the lines of
/[A-Z]/
or close to that

- Mac
--
Posted via http://www.ruby-....

ara.t.howard

11/8/2007 6:00:00 PM

0


On Nov 7, 2007, at 9:52 PM, Thilina Buddhika wrote:

> I want to get the names of the classes defined in a Ruby script. Is
> there any possible way of doing this ?
>
> thanks!
>
> regs,
> buddhika
> --
> Posted via http://www.ruby-....
>

the basis is:

cfp:~ > cat a.rb
class A;end
class B;end
class C < B; end

p Class.es

BEGIN {
class Class
ES = []
def es() ES end
def inherited(other) es << other end
end
}


cfp:~ > ruby a.rb
[A, B, C]



a @ http://codeforp...
--
it is not enough to be compassionate. you must act.
h.h. the 14th dalai lama




7stud --

11/8/2007 6:49:00 PM

0

Thilina Buddhika wrote:
> I want to get the names of the classes defined in a Ruby script. Is
> there any possible way of doing this ?
>

Try this:

class Class
Seen_classes = []

def inherited(class_obj)
Seen_classes << class_obj.name
end

def Seen_classes
Seen_classes
end
end

class Dog
end

class Monkey
end

module Food
end

PI = 3.14

def meth1
end


p Class.Seen_classes

--output:--
["Dog", "Monkey"]
--
Posted via http://www.ruby-....

Thilina Buddhika

11/9/2007 3:24:00 AM

0


Hi friends,
It works fine,...thanks a lot !!!!!

regs,
buddhika
--
Posted via http://www.ruby-....