Created
August 30, 2012 15:03
-
-
Save tkawachi/3530304 to your computer and use it in GitHub Desktop.
Gon に Ruby の Module で定義された定数を渡す
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Const | |
| ABC = 1 | |
| module CategoryX | |
| XYZ = 2 | |
| end | |
| # Gon で JavaScript, CoffeeScript に渡せるように定数を hash に変換する | |
| # @param [Module] target | |
| def to_hash(target = self) | |
| result = {} | |
| target.constants.each do |const_sym| | |
| const_val = target.const_get(const_sym) | |
| result[const_sym] = const_val.class == Module ? to_hash(const_val) : const_val | |
| end | |
| result | |
| end | |
| module_function :to_hash | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment