Topic: Convert String to Variable

I have several methods that would benefit from this. Is there a built-in method in Ruby for converting "string" into @string?

My googling has been fruitless so far.

Edit: I should clarify that I am trying to dynamically set a singular instance variable in my show actions across my application. If the current controller is ArticlesController, I would like my method to return @article

Last edited by pimpmaster (2007-05-07 20:21:25)

Re: Convert String to Variable

How about using instance_variable_set?

def initialize
  iv_name = name.split(/(.+)sController/)[1].downcase
  instance_variable_set("@#{iv_name}", eval(iv_name, whatever_you_want_it_set_to))
end

It's a quick little hack (you may want to fix up the regex), but it works.

(Actually, it might not. whoops...still looking. tongue)

EDIT
Ok, so my Ruby Cookbook says:

class Object
  private
  def set_instance_variables(binding, *variables)
    variables.each do |var|
      instance_variable_set("@#{var}", eval(var, binding))
    end
  end
end

class RGBColor
  def initialize(red=0, green=0, blue=0)
    set_instance_variables(binding, *local_variables)
  end
end

p RGBColor.new(10,200,300)


Take from that what you will. smile

EDIT2
VVV Or do what ryan says. VVV wink

Last edited by Firanide (2007-05-07 21:12:49)

Re: Convert String to Variable

I've done this in the past:

  # in controller
  before_filter :find_or_initialize, :only => [:show, :new, :create, :edit, :update, :destroy]
 
  protected

    def find_or_initialize
      if params[:id].nil?
        instance_variable_set "@#{model_name}", model_class.new
      else
        instance_variable_set "@#{model_name}", model_class.find(params[:id])
      end
    end

    def model_name
      params[:controller].singularize
    end
 
    def model_class
      model_name.camelize.constantize
    end

Railscasts - Free Ruby on Rails Screencasts

Re: Convert String to Variable

This is interesting.. I suppose I should post a code example as I think I am trying to do something a little different

def owner
    var = controller_name.singularize
    @creator = User.find_by_id(@var.created_by)
    @updater = User.find_by_id(@var.updated_by)
end

of course that code is terribly butchered but it should give you a clearer idea of the problem

Re: Convert String to Variable

Got it!

def model_name
    params[:controller].singularize.capitalize
  end
 
  def owner
    @var = model_name.constantize.find_by_permalink(params[:permalink])
    @creator = User.find_by_id(@var.created_by)
    @updater = User.find_by_id(@var.updated_by)
  end

This calls for some beer smile

Re: Convert String to Variable

Hmm.. turnsout I celebrate too soon.. this obviously only works on models with permalinks.

I am surprised there is no easy way to just convert to instance variable. It would be sweet if I could just do "string".to_instance_variable

Re: Convert String to Variable

pimpmaster wrote:

Hmm.. turnsout I celebrate too soon.. this obviously only works on models with permalinks.

Here's an idea:

if params[:permalink]
  @var = model_name.constantize.find_by_permalink(params[:permalink])
else
  @var = model_name.constantize.find(params[:id])
end

pimpmaster wrote:

I am surprised there is no easy way to just convert to instance variable. It would be sweet if I could just do "string".to_instance_variable

An instance variable is only available to the scope of that object, so a String can't set the instance variable of some other controller/object. Unless you pass the bindings around but that is more of a pain then the set_instance_variable method I think.

Railscasts - Free Ruby on Rails Screencasts

Re: Convert String to Variable

That did the trick! Thanks for helping me stay DRY

smile