From: Robert Klemme Date: 2010-02-12T17:59:10+09:00 Subject: Re: String to symbol 2010/2/12 Sergey Sheypak : > Hello, I'm working with yaml and objects. > Please, see my model: > > #Base class for all models. > class DataBean >  #list allowed fields for model >  def allowed_fields >    [] >  end > >  def is_attr_allowed!(attr) IMHO that method would rather be called ensure_attr_allowed - if you call it is_... I would expect it to return a boolean. >    raise(ValidationException, "Attribute with name [#{attr}] is not > allowed for model #{self.class}. >                                     Allowed fields are > #{allowed_fields}", caller) unless allowed_fields.include?(attr) >  end > >  def initialize(options) >      options.each_pair { |attr_name , value| >        is_attr_allowed!(attr_name) >        self.class.send(:define_method, attr_name, Proc.new{value}) >      } >  end Are you sure this is what you want? It seems you change the accessor method for *all* instances to return the *same* value. I don't think this is a good idea. > end > > class Dates < DataBean >  def allowed_fields >    [:start,  #project beginning >     :end,    #project ending >     :current] #last modification date "T", means current date >  end > end > > So I can use 'rails like intializers' > Dates.new(:start=>Date.new, :end=>Dates.new+100) > > The problem is that I'm reading Dates class fields from yaml file. > > Dates: >  class: Dates >  fields_ordered: [start, current, end] > > And I pass String (read from yaml) as a hash key to initializer: > Dates.new('start'=>Date.new) > > As I read, :symbols are constants, they are immutable and have int > representation. These facts help to spend less memory and resources. > > Do I have any opportunity to pass Strings into my initializers? Did you try it out with your code? Was there an issue, if so, which one? Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/