0

Is there a better way to set values to setter methods when they are made dynamically using attr_accessor method? I need this for setting values for them from another model in rails. I'm trying to do something like below.

Model_class.all.each do |mdl|
  attr_accessor(mdl.some_field)
end

Then I know that it creates a set of get and setter methods. What I want to do is, when these methods are get created, i want some value to be specified for setter method.Thanks in advance.

2
  • i don't know what you are asking for, but my intuition says that you are looking for delegate methods - apidock.com/rails/Module/delegate Commented Jan 31, 2017 at 6:04
  • Better than what? Commented Jan 31, 2017 at 11:14

2 Answers 2

2

attr_accessor has no magic embedded. For each of params passed to it, it basically executes something like (the code is simplified and lacks necessary checks etc):

def attr_accessor(*vars)
  vars.each do |var|
    define_method var { instance_variable_get("@#{var}") }
    define_method "#{var}=" { |val| instance_variable_set("@#{var}", val) }
  end
end

That said, the attr_accessor :var1, :var2 DSL simply brings new 4 plain old good ruby methods. For what you are asking, one might take care about defining these methods (or some of them, or none,) themselves. For instance, for cumbersome setting with checks one might do:

attr_reader :variable # reader is reader, no magic

def variable=(val) do
  raise ArgumentError, "You must be kidding" if val.nil?
  @variable = val
end

The above is called as usual:

instance.variable = 42
#⇒ 42
instance.variable = nil
#⇒ ArgumentError: You must be kidding
Sign up to request clarification or add additional context in comments.

Comments

0

Here is another possible implementation for this:

  def attr_accessor(*args)
    args.each do |attribute|
      define_method(attribute.to_sym) { eval("@#{attribute}") }
      define_method((attribute.to_s + '=').to_sym) {|value| eval("@#{attribute} = value") }
    end
  end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.