Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 641 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I convert a Ruby class name to a underscore-delimited symbol?

#1
How can I programmatically turn a class name, `FooBar`, into a symbol, `:foo_bar`? e.g. something like this, but that handles camel case properly?

FooBar.to_s.downcase.to_sym
Reply

#2
**Rails 4 `.model_name`**

In Rails 4, it returns an [`ActiveModel::Name`](

[To see links please register here]

) object which contains many useful more "semantic" attributes such as:

FooBar.model_name.param_key
#=> "foo_bar"

FooBar.model_name.route_key
#=> "foo_bars"

FooBar.model_name.human
#=> "Foo bar"

So you should use one of those if they match your desired meaning, which is likely the case. Advantages:

- easier to understand your code
- your app will still work even in the (unlikely) event that Rails decides to change a naming convention.

BTW, `human` has the advantage of being I18N aware.
Reply

#3
Here's what I went for:

module MyModule
module ClassMethods
def class_to_sym
name_without_namespace = name.split("::").last
name_without_namespace.gsub(/([^\^])([A-Z])/,'\1_\2').downcase.to_sym
end
end

def self.included(base)
base.extend(ClassMethods)
end
end

class ThisIsMyClass
include MyModule
end

ThisIsMyClass.class_to_sym #:this_is_my_class
Reply

#4
Rails comes with a method called `underscore` that will allow you to transform CamelCased strings into underscore_separated strings. So you might be able to do this:

FooBar.name.underscore.to_sym

But you will have to install ActiveSupport just to do that, as ipsum says.

If you don't want to install ActiveSupport just for that, you can monkey-patch `underscore` into `String` yourself (the underscore function is defined in [ActiveSupport::Inflector][1]):

class String
def underscore
word = self.dup
word.gsub!(/::/, '/')
word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
word.tr!("-", "_")
word.downcase!
word
end
end


[1]:

[To see links please register here]

Reply

#5
first: gem install activesupport

require 'rubygems'
require 'active_support'
"FooBar".underscore.to_sym
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through