Skip to content

Instantly share code, notes, and snippets.

@jcalvert
Created June 15, 2016 20:38
Show Gist options
  • Save jcalvert/dd919e98fbba0cf085918e7ff6473302 to your computer and use it in GitHub Desktop.
Save jcalvert/dd919e98fbba0cf085918e7ff6473302 to your computer and use it in GitHub Desktop.
constants to class methods
constants(false).reject{|symbol| symbol.to_s =~ /[a-z]/}.each do |constant|
name = constant.to_s.downcase.gsub(/_name/, '')
var_name = "@#{name}"
define_singleton_method(name) do
unless instance_variable_defined?(var_name)
instance_variable_set(var_name, find_by(:name => const_get(constant)))
end
instance_variable_get(var_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment