Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 837203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:06:32+00:00 2026-05-15T05:06:32+00:00

So I know you can say Kernel.const_get(ClassName) and you’ll get back the class to

  • 0

So I know you can say Kernel.const_get(“ClassName”) and you’ll get back the class to which the string corresponds in name. But what about for variables? Is there a way to do:

test = "heyas"
some_method_here("test") #=> "heyas"

Thanks so much

The fact is that I need it in more complex code, real example:

class User
  class Validations
    class << self
      def username_len
        return u.len > 3 and u.len < 22
      end
      # ...
    end
  end
  def validate(u,e,p)
    [:u, :e, :p].each do |v|
      Validations.send(:define_singleton_method, v, lambda { eval(v.to_s) }) # see this line
    end
    #other code to run validations
  end
end

So you see, there’s no better way, is there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T05:06:33+00:00Added an answer on May 15, 2026 at 5:06 am

    No, there is only class_variable_get, instance_variable_get and const_get. There is no local_variable_get. But it wouldn’t make sense anyway: local variables are local to the current method body, module body, class body or script body, that’s why they are called “local” variables, after all! You simply cannot access them from another method.

    Is there a way to do:

    test = "heyas"
    some_method_here("test") #=> "heyas"
    

    No, there is no way to do this. And there cannot possibly be a way to do it. test is a local variable, which means it only exists in the current script body. It does not exist inside the body of some_method_here. That’s the whole point of local variables: you cannot ever, under any circumstances, access them from somewhere else.

    Regarding your comment on another answer:

    def my_func(str)
      var_a = 'hey'
      var_b = 'ah'
      return some_method_here(str)
    end
    #=> should return the corresponding variable's value, e.g. my_func('var_a')
    #=> 'hey'
    

    Again, this cannot possibly work, since the whole point of local variables is that they cannot be accessed from anywhere else.

    But there is a pretty simple variation which does exactly what you want:

    def my_func(str)
      {
        'var_a' => 'hey',
        'var_b' => 'ah'
      }[str]
    end
    #=> should return the corresponding variable's value, e.g. my_func('var_a')
    #=> 'hey'
    

    This can of course be further simplified to:

    my_func = {'var_a' => 'hey', 'var_b' => 'ah'}
    #=> should return the corresponding variable's value, e.g. my_func['var_a']
    #=> 'hey'
    

    Given that you can only pass in a limited number of different options, it’s probably better to use symbols instead:

    my_func = {var_a: 'hey', var_b: 'ah'}
    #=> should return the corresponding variable's value, e.g. my_func[:var_a]
    #=> 'hey'
    

    What you are asking is basically: pass in a key, get a value out. That’s exactly what a Hash is.

    EDIT: After the revised question, this is the best that I could come up with:

    def validate(u, e, p)
      local_variables.zip(local_variables.map {|var|
        eval(var.to_s)
      }).each {|var, val|
        Validations.send(:define_singleton_method, var) { val }
      }
    end
    

    However, I think there is something seriously wrong with the design. You overwrite singleton methods of User::Validations based on different instances of User. By the very definition of singleton method, there can only ever be one copy of those in the system. But you have many different instances of User, and every time you call User#validate it will overwrite the only copies of User::Validations.u, User::Validations.e and User::Validations.p at which point they will start to behave completely differently for the entire system.

    In other words, you are changing the behavior of the entire system based on a single instance. And there can be many instances and every time, the behavior of the system changes.

    That just can’t be right.

    u1 = User.new
    u1.validate('u1', :e1, 1)
    
    p User::Validations.u, User::Validations.e, User::Validations.p
    # => 'u1'
    # => :e1
    # => 1
    
    u2 = User.new
    u2.validate('U2', :E2, 2.0)
    # =>  warning: method redefined; discarding old u
    # =>  warning: method redefined; discarding old e
    # =>  warning: method redefined; discarding old p
    #              ^^^^^^^^^^^^^^^^
    #                    Oops!
    
    p User::Validations.u, User::Validations.e, User::Validations.p
    # => 'U2'
    # => :E2
    # => 2.0
    #    ^^^
    # Completely different results for the exact same arguments
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know in rails i can say resources :articles do post :something end but
i know i can have iframe in a html page, say parent.htm, and i
I have a query that I know can be done using a subselect, but
You can know if the event stack is empty calling the gtk.events_pending() method, but
I know you can not set a key value dynamically, but what about the
I know the app delegate can say yo man, I want thais new landscape
I know that I can accomplish this via iterating over the first range, but
I can say I don't know what I'm asking for help,because I don't know
I don't know how much I can say about the app that I am
I would like to know if I can install say Visual Studio 2008 Pro

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.