# Accessing Class from class symbol

**URL:** <https://www.ruby-forum.com/t/accessing-class-from-class-symbol/66110>\
**Category:** Ruby\
**Created:** [February 12, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/accessing-class-from-class-symbol/66110 "2006-02-12T12:08:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![treefrog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@treefrog](https://www.ruby-forum.com/u/treefrog)\
**Post date:** [February 12, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/accessing-class-from-class-symbol/66110/1 "2006-02-12T12:08:35Z")

</div>

Hi,  
I’m trying to work out how to access Class methods when I only have the  
class symbol

class Foo  
def Foo.hello  
puts “Hello Foo”  
end  
end

class Bar  
def Bar.hello  
puts “Hello Bar”  
end  
end

str=“Foo”

# I could do this, but there must be a better way.

eval “#{str}.hello”

Kernel doesn’t have send, so I can’t pass the class as a symbol to  
Kernel, and then send the method.

Any ideas?

Cheers

Steve

---

<div class="post-metadata">

**Author:** ![treefrog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c68b51/32.png) [@treefrog](https://www.ruby-forum.com/u/treefrog)\
**Post date:** [February 12, 2006, 1:18pm UTC](https://www.ruby-forum.com/t/accessing-class-from-class-symbol/66110/2 "2006-02-12T13:18:33Z")

</div>

“treefrog” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> class Bar  
> Kernel doesn’t have send, so I can’t pass the class as a symbol to  
> Kernel, and then send the method.

Kernel does have `send’, but classes aren’t methods… 🙂

Foo and Bar in your case are constants. Since they’re defined at  
toplevel scope (i.e., not nested in any class or module definitions),  
they’re under Object. So to get the class, do:

klass = Object.const\_get(‘Foo’) # symbols also accepted

And to call a method, do:

klass.send(‘hello’) # symbols also accepted

Hope this helps!
