getting the names of classes defined in a ruby script

  • Thread starter Thilina Buddhika
  • Start date
T

Thilina Buddhika

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
 
M

Michael Linfield

Thilina said:
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
 
A

ara.t.howard

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

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://codeforpeople.com/
 
7

7stud --

Thilina said:
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"]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top