Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to variable name in ruby

Tags:

ruby

I have variables

 <% mon_has_two_sets_of_working_hours = 0 %>
 <% tue_has_two_sets_of_working_hours = 0 %>
 <% wed_has_two_sets_of_working_hours = 0 %>

I want to change the values of these variables dynamically.

 <% days_array = ['mon', 'tue', 'wed'] %>

 <% days_array.each do |day| %>
   <% if condition? %>
    # here i want to set %>
     <% "#{day}__has_two_sets_of_working_hours" = 1 %>
  end
 end

The value is not getting assigned. Is there any way to assign value to variable dynamically?

like image 895
srivani Avatar asked Jan 08 '11 12:01

srivani


People also ask

How do you declare a string variable in Ruby?

A string in Ruby is an object (like most things in Ruby). You can create a string with either String::new or as literal (i.e. with the double quotes "" ). But you can also create string with the special %() syntax With the percent sign syntax, the delimiters can be any special character.

How to convert string to int Ruby?

To convert an string to a integer, we can use the built-in to_i method in Ruby. The to_i method takes the string as a argument and converts it to number, if a given string is not valid number then it returns 0.

How to convert string to float in Ruby?

Converting Strings to Numbers Ruby provides the to_i and to_f methods to convert strings to numbers. to_i converts a string to an integer, and to_f converts a string to a float.

What is variable name in Ruby?

Local Variables: A local variable name always starts with a lowercase letter(a-z) or underscore (_). These variables are local to the code construct in which they are declared. A local variable is only accessible within the block of its initialization. Local variables are not available outside the method.


2 Answers

I don't think there is a way to do this. There is with instance or class variables, but with local variables there is very rarely a good need.

In your case you really should have the data in a hash. Also, logic like this really does not belong in erb. You want something like:

working_hour_sets = %w[mon tue wed thu fri sat sun].inject({}) do |hash, day|
  hash[day]=0;
  hash
end
# puts working_hour_sets #=> {"wed"=>0, "sun"=>0, "thu"=>0, "mon"=>0, "tue"=>0, "sat"=>0, "fri"=>0}

working_hour_sets.each do |day, value|
  working_hour_sets[day] = 1 if condition?
end
like image 78
gunn Avatar answered Sep 27 '22 17:09

gunn


Now, I know this question is a bit old, but there is an easier way to do this and is using the standard Ruby send method. This is actually one of the methods that make Ruby so agile in the metaprogramming world.

This is actually a config setting I use in a Rails app:

# In a YAML    
twitter:
  consumer_key: 'CONSUMER-KEY'
  consumer_secret: 'CONSUMER-SECRET'
  oauth_token: 'OAUTH-KEY'
  oauth_token_secret: 'OAUTH-SECRET'

...

# And in your file.rb
config = YAML.load_file(Rails.root.join("config", "social_keys.yml"))[Rails.env]['twitter']

Twitter.configure do |twitter|
  config.each_key do |k|
    twitter.send("#{k}=", config[k])
  end
end

It's DRY and very easy to understand. :)

like image 38
betacar Avatar answered Sep 27 '22 19:09

betacar