-1

I have a hash like this:

$login_form = {
    $username => {web_element_type: :id, web_element: 'uname'},
    $password => {web_element_type: :id, web_element: 'pass'}
}

How can i retrieve $password's web element type and web element with the simplest way?

buurkeey
  • 369
  • 2
  • 5
  • 19
  • 2
    I would suggest you to go read some intro on ruby syntax. Ruby differs from PHP. One can not simply expect the syntax of one language will be applied everywhere. – Aleksei Matiushkin Jan 25 '17 at 13:10

2 Answers2

1

Global variables ?

Are you sure you need global variables?

Right now, the keys in your hash aren't string, symbol or integers, but the object referenced by the variables $username and $password. Those variables are global, and are accessible from everywhere in your code. This doesn't seem to be a good idea for a variable called password.

If those variables aren't initialized, they both are nil, so your login_form is actually :

{nil=>{:web_element_type=>:id, :web_element=>"pass"}}

The values for $username have been overwritten by $password.

If you're sure that login_form should be a global variable :

$login_form = {
    username: {web_element_type: :id, web_element: 'uname'},
    password: {web_element_type: :id, web_element: 'pass'}
}

p $login_form[:password].values_at(:web_element_type,  :web_element)
#=> [:id, "pass"]
type, element = $login_form[:password].values_at(:web_element_type,  :web_element)
p type
#=> :id
p element
#=> "pass"

If not, you could use @login_form or just login_form.

If you're still confused, you could look at this.

Community
  • 1
  • 1
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
1

This solution can help you and ready Hash

define hash like this way

$login_form = {
  :username => {web_element_type: :id, web_element: 'uname'},
  :password => {web_element_type: :id, web_element: 'pass'}
}

you can retrieve password like this way

$login_form[:password] 

If you have any question please me know.

Sonu Singh
  • 305
  • 1
  • 10