Skip to content

Instantly share code, notes, and snippets.

@tkawachi
Created August 30, 2012 15:03
Show Gist options
  • Select an option

  • Save tkawachi/3530304 to your computer and use it in GitHub Desktop.

Select an option

Save tkawachi/3530304 to your computer and use it in GitHub Desktop.
Gon に Ruby の Module で定義された定数を渡す
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