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

  • Home
  • SEARCH
  • 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 9233377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:32:22+00:00 2026-06-18T06:32:22+00:00

This is primarily a Ruby question but I’ve tagged it with watir-webdriver because the

  • 0

This is primarily a Ruby question but I’ve tagged it with watir-webdriver because the example includes watir-webdriver code in the hopes that it will improve clarity.

I have a class that can both retrieve and update data found on a web page. That data gets stored in instance variables of the class.

The class includes a method that will either use the existing instance variable value to update a select list on the web page, or, if the instance variable is nil, it will get the select list’s value and store it in the instance variable.

The method currently looks like this:

def get_or_select!(inst_var_sym, select_list)
  value = instance_variable_get inst_var_sym
  if value==nil
    instance_variable_set inst_var_sym, select_list.selected_options[0].text
  else
    select_list.select value
  end
end

That works, but I am wondering if there is a way to write the method such that it can be applied directly to the instance variable (and not a symbol matcher for the instance var) and take, as its single parameter, the select_list object.

In other words, what currently looks like this:

get_or_select!(:@instance_variable, select_list)

I would like to look like this:

@instance_variable.get_or_select!(select_list)

Is this even possible?

  • 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-06-18T06:32:23+00:00Added an answer on June 18, 2026 at 6:32 am

    New answer:
    Since the variable doesn’t exist until you define it, you cannot call methods on it. The calling class will have to set it. You can just do a simple if check.

    #In the calling class, not the variable class  
      if @instance_variable       # If it is defined, this is true
        returned_list = select_list.select @instance_variable.value # Gets the selected list 
      else
        @instance_variable = instance_variable_set inst_var_sym, select_list.selected_options[0].text
      end
    

    OLD answer below:
    Could you not use the conditional assignment?
    http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators#1._Assignment

    x = find_something() #=>nil
    x ||= "default"      #=>"default" : value of x will be replaced with "default", but only if x is nil or false
    x ||= "other"        #=>"default" : value of x is not replaced if it already is other than nil or false
    

    so for this it would be something like…

    instance_variable_get inst_var_sym ||= instance_variable_set inst_var_sym, select_list.selected_options[0].text
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is primarily a question of possibilities more than instructions. I'm a programming consultant
This is a question primarily about Chef. When looking into controlling nodes inside Amazon
This seems to be primary Question. but i search it and i couldn't find
I am primarily a web developer (ruby, python) that is new to iPhone development.
This should be a very basic question, I'm still new to ruby so i'd
I'm trying to precompile my rails app but get this weird error that I
I'm working on some Ruby code that needs to create primary keys on existing
I am using WCF RIA services with silverlight. Obviously I am using this primarily
This is primarily a curiosity, I'm not really sure what's the practical use of
This has probably been answer already but I am trying to return the primary

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.