1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-04 23:58:01 +00:00

Add Construct A Constant From A String as a ruby til.

This commit is contained in:
jbranchaud
2015-09-23 20:49:42 -05:00
parent e116e1bf54
commit f1b14eec12
2 changed files with 29 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
# Construct A Constant From A String
Ruby's
[`Module.const_get`](http://ruby-doc.org/core-2.1.0/Module.html#method-i-const_get)
can be used to look for and retrieve the constant for the given name.
This can be used to construct a class name
```ruby
> Object.const_get("Math")
#=> Math
> Object.const_get("Math")::PI
#=> 3.141592653589793
```
It can also be used to reference a constant
```ruby
> Object.const_get("Math::PI")
#=> 3.141592653589793
```
You can even be more specific if you'd like
```ruby
> Math.const_get("PI")
#=> 3.141592653589793
```