字符串与对象的转化

classify: http://apidock.com/rails/Inflector/classify

  "egg_and_hams".classify # => "EggAndHam"
  "posts".classify        # => "Post"

 

constantize:http://apidock.com/rails/Inflector/constantize

  "Module".constantize     # => Module
  "Test::Unit".constantize # => Test::Unit

 

tableize: http://apidock.com/rails/Inflector/tableize

 

 "RawScaledScorer".tableize # => "raw_scaled_scorers"
  "egg_and_ham".tableize     # => "egg_and_hams"
  "fancyCategory".tableize   # => "fancy_categories"

 

 

instance_variable_get : http://apidock.com/ruby/Object/instance_variable_get 

 

class Fred
  def initialize(p1, p2)
    @a, @b = p1, p2
  end
end
fred = Fred.new('cat', 99)
fred.instance_variable_get(:@a)    #=> "cat"
fred.instance_variable_get("@b")   #=> 99
 

 

instance_variable_set: http://apidock.com/ruby/Object/instance_variable_set

 

class Fred
  def initialize(p1, p2)
    @a, @b = p1, p2
  end
end
fred = Fred.new('cat', 99)
fred.instance_variable_set(:@a, 'dog')   #=> "dog"
fred.instance_variable_set(:@c, 'cat')   #=> "cat"
fred.inspect         
 

 

send: http://apidock.com/ruby/Object/send

 

class Klass
  def hello(*args)
    "Hello " + args.join(' ')
  end
end
k = Klass.new
k.send :hello, "gentle", "readers"   #=> "Hello gentle readers"
 

 

pluralize:http://apidock.com/rails/String/pluralize

 

'post'.pluralize             # => "posts"
'octopus'.pluralize          # => "octopi"
'sheep'.pluralize            # => "sheep"
 

 

singularize:http://apidock.com/rails/String/singularize

 

'posts'.singularize            # => "post"
'octopi'.singularize           # => "octopus"
'sheep'.singularize            # => "sheep"
'word'.singularize             # => "word"
 

你可能感兴趣的:(字符串与对象的转化)