[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Reflection and files

Gavin Kistner

9/11/2006 11:06:00 PM

> Suppose I have a ruby file with a single class. Is it
> possible to write a
> ruby script that can extract the class name from the file?
[...]
> Is it possible to extract the class name via reflection?

Well, here's one terrible idea. (It's 'terrible' because it's brittle,
and only works if no other files defining new classes have been defined,
and will break if a new Ruby release defines a new class.)

BUILTIN_CLASSES = %w| ArgumentError Array Bignum Binding Class
Continuation
Data Date DateTime Dir EOFError Exception FalseClass File Fixnum Float
FloatDomainError Hash IO IOError IndexError Integer Interrupt LoadError
LocalJumpError MatchData MatchData Method Module NameError NilClass
NoMemoryError NoMethodError NotImplementedError Numeric Object Proc
Range
RangeError Rational Regexp RegexpError RuntimeError ScriptError
SecurityError SignalException StandardError String Struct Symbol
SyntaxError SystemCallError SystemExit SystemStackError Thread
ThreadError ThreadGroup Time TrueClass TypeError UnboundMethod
ZeroDivisionError |.map{ |n| Object.const_get( n ) }

class Foo; end

nonstandard_classes = Object.constants.map{ |name|
klass = Object.const_get( name )
Class === klass ? klass : nil
}.compact - BUILTIN_CLASSES

puts nonstandard_classes
#=> Foo

5 Answers

Rodrigo Kochenburger

9/12/2006 2:06:00 AM

0

Wouldn't be easier to give the file the same name of the class?
Like some_class.rb to the file and SomeClass to the class?

I don't know what you're trying, so just a thought.

Cheers

MonkeeSage

9/12/2006 8:35:00 AM

0

Gavin Kistner wrote:
> Well, here's one terrible idea. (It's 'terrible' because it's brittle,
> and only works if no other files defining new classes have been defined,
> and will break if a new Ruby release defines a new class.)

How about:

def get_class_from_file(file)
consts = Object.constants
require file
return (Object.constants - consts)[0]
end

p get_class_from_file('/path/to/some_file.rb')

Regards,
Jordan

Joel VanderWerf

9/12/2006 5:59:00 PM

0

MonkeeSage wrote:
> Gavin Kistner wrote:
>> Well, here's one terrible idea. (It's 'terrible' because it's brittle,
>> and only works if no other files defining new classes have been defined,
>> and will break if a new Ruby release defines a new class.)
>
> How about:
>
> def get_class_from_file(file)
> consts = Object.constants
> require file
> return (Object.constants - consts)[0]
> end
>
> p get_class_from_file('/path/to/some_file.rb')

Doesn't work if file requires some other file (such as a ruby std lib
file). Also, not thread safe. Also, the class may be defined as

class Foo::Bar
end

and it will not show up in Object.constants.

Also, there might be other constants:

K=1
class Foo; end

It's a hard problem in ruby...

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Stefan Mahlitz

9/12/2006 6:10:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Gavin Kistner wrote:
>> Suppose I have a ruby file with a single class. Is it
>> possible to write a
>> ruby script that can extract the class name from the file?
> [...]
>> Is it possible to extract the class name via reflection?
>
> Well, here's one terrible idea. (It's 'terrible' because it's brittle,
> and only works if no other files defining new classes have been defined,
> and will break if a new Ruby release defines a new class.)

I agree.

I just wanted to share what I had in mind, which will not break because
of a new ruby version (I guess):

old_classes = self.class.constants.find_all {|x|
self.class.const_get(x).class == Class }.compact

require'my_lib'

all_classes = self.class.constants.find_all {|x|
self.class.const_get(x).class == Class }.compact

new_classes = (all_classes - old_classes).collect {|x|
self.class.const_get(x) }

Stefan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail....

iD8DBQFFBveV9S2Eui6zfdQRAlAOAKCjHM1eUg3ueMnoQ7krW2D5RBBmxgCdHyTs
olOXERUvErsSh6GxLhC8s34=
=UYzZ
-----END PGP SIGNATURE-----

David Vallner

9/14/2006 10:44:00 AM

0

MonkeeSage wrote:
> How about:
>
> def get_class_from_file(file)
> consts = Object.constants
> require file
> return (Object.constants - consts)[0]
> end
>

Also doesn't work if the file was already required before.

David Vallner