Ruby Variable assignment

Hello, I want to assign my variable ‘a’ some value as follows:

a = “:name, btnK”

and use this variable ‘a’. when I do this and use the variable, I get
error as “no implicit conversion of String into Hash”…

Aprreciate a response

Thanks,
#M

Without showing the exact code triggering the error, there is no way we
can help. Your variable and its contents are not special enough to show
an error per se:

2.0.0p247 :001 > a = “:name, btnk”
=> “:name, btnk”
2.0.0p247 :002 > puts a
:name, btnk
=> nil

Eric MSP Veith wrote in post #1125861:

Without showing the exact code triggering the error, there is no way we
can help. Your variable and its contents are not special enough to show
an error per se:

2.0.0p247 :001 > a = “:name, btnk”
=> “:name, btnk”
2.0.0p247 :002 > puts a
:name, btnk
=> nil

Following is the content of my files:

File1.rb
require “rubygems”
require “watir”

class GoogleHomePage

attr_reader :shaik, :googlesearchbutton, :hindilinkclick,
:bengalilinkclick, :searchfield

def initialize()
puts “in INITIALIZE”

@googlesearchbutton = “:name, btnK”

end

File2.rb

require “rubygems”
require “watir”
#require ‘watir-webdriver’
require ‘rspec’

Given /^that I have gone to the (.+)$/ do |page_name|
puts “IN Given”

@@googlehomepageobject = GoogleHomePage.new() #Instance of class in
File1.rb

puts “------------ #{@@googlehomepageobject.googlesearchbutton}” #this
line prints the value of the variable as “:name, btnK”

@browser.button(@@googlehomepageobject.googlesearchbutton).wait_until_present
#tis line triggers the error “no implicit conversion of String into
Hash”

end

Thanks,
#M

Watir expects Hash arguments, not String arguments.

This is wrong:
@browser.button(":name, btnK").wait_until_present

This is right:
@browser.button(name: “btnK”).wait_until_present

If you change this:
@googlesearchbutton = “:name, btnK”
to this:
@googlesearchbutton = { name: “btnK” }
it should work.

Works for me:

irb(main):006:0> b.button( name: “btnK” ).flash
=> #<Watir::Button:0x…fe422fdee located=true selector={:name=>“btnK”,
:tag_name =>“button”}>
irb(main):007:0> h = { name: “btnK” }
=> {:name=>“btnK”}
irb(main):008:0> b.button( h ).flash
=> #<Watir::Button:0x…fe422fdee located=true selector={:name=>“btnK”,
:tag_name =>“button”}>

Joel P. wrote in post #1125866:

Watir expects Hash arguments, not String arguments.

This is wrong:
@browser.button(":name, btnK").wait_until_present

This is right:
@browser.button(name: “btnK”).wait_until_present

If you change this:
@googlesearchbutton = “:name, btnK”
to this:
@googlesearchbutton = { name: “btnK” }
it should work.

=======

I made the changes, defined the variable as @googlesearchbutton = {
name: “btnK” } but, after this is any change required to the line which
calls the variable as below

@browser.button(@@googlehomepageobject.googlesearchbutton).wait_until_present
Still getting the error as
no implicit conversion of String into Hash (TypeError)
(eval):3:in `button’

Thx,
#M

Sorry, it was my mistake Eric, your suggestion worked.

You must have required the file at some point or you would get an
uninitialised constant error.

File1.rb
require “rubygems”
require “watir”

class GoogleHomePage

attr_reader :shaik, :googlesearchbutton, :hindilinkclick,
:bengalilinkclick, :searchfield

def initialize()
puts “in INITIALIZE”

@googlesearchbutton = “:name, btnK”

end

File2.rb

require “rubygems”
require “watir”
#require ‘watir-webdriver’
require ‘rspec’

Given /^that I have gone to the (.+)$/ do |page_name|
puts “IN Given”

@@googlehomepageobject = GoogleHomePage.new() #Instance of class in
File1.rb

puts “------------ #{@@googlehomepageobject.googlesearchbutton}” #this
line prints the value of the variable as “:name, btnK”

@browser.button(@@googlehomepageobject.googlesearchbutton).wait_until_present
#tis line triggers the error “no implicit conversion of String into
Hash”

end

Thanks,
#M
===========

Eric, I got a different question here,in File2.rb, I’ve created an
instance of the class in File1.rb, I’m confused how ruby does this
though I didn’t require File1.rb in File2.rb, is require not mandatory
to create an instance of a class?

Thanks,
#M

Joel P. wrote in post #1125877:

You must have required the file at some point or you would get an
uninitialised constant error.

I required the file in File2.rb but I commented the line. Not sure how
it works.

Thx,
#M

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs