Re: Reflection and files

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

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

Gavin K. 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

MonkeeSage wrote:

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…

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 V.

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

Gavin K. 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.mozdev.org

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