ar.dll
ar.dll

Reputation: 787

ruby passing the value of a hash to a method

I'm new to ruby but I'm experimenting with it using watir-webdriver. I'm trying to work out how I can pass a hash value to a method like so:

#this is a hash in ruby - a collection of keys and values
title = { :Mr => "Mr", :Mrs => "Mrs", :Miss => "Miss", :Ms => "Ms", :Other = > "Other"}

def select_title(title)
 @browser.element(:xpath => "//input[@type='radio'][@value='Value of title hash']").click
end

I don't want to write an if then else or switch like logic inside the method, just pass the value of the hash straight into the xpath? how?

Upvotes: 0

Views: 348

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

If I understood the question correctly, here you go:

def select_title(title_hash_key)
  @browser.element(:xpath => "//...[@value='#{title[title_hash_key]}']")
          .click
end

Now when the hash key is passed to this method as an argument, the respective value will be fetched from the hash and put into xpath string using string interpolation (#{title[title_hash_key]}.)

Upvotes: 1

Related Questions