Prashant Sahni

Web developer

Ruby 'Const-missing' Tip

Handle loading of constants with the help of const_missing

We can find the constants defined for the class on the fly like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class User
  def self.const_missing(const_name)
   case const_name
    when :PREMIUM
      1
    when :GOLD
      2
    when :PLATINUM
      3
    when :TITANIUM
      4
   else
     nil
   end
  end
end

# User::PREMIUM =>  1
# User::GOLD    =>  2
# User::PLATINUM => 3
# User::DIAMOND => nil

You can set constant value on the fly like this:

1
self.const_set(const_name, -rand(99999)) unless self.const_defined?(const_name)
So, Instead of returning nil you can write above code, depending upon our usecase.

We can do lots of interesting tasks with the help of this technique, like loading the file on the fly or including module and calling its method. Think about it. Hope it helps. Thanks for reading.

Comments