Forum: Ruby Ruby Variable assignment

462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-28 12:44
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
2ebf2af9aa9347e30775d77eabbcc6c7?d=identicon&s=25 Eric MSP Veith (ericmsp_v)
on 2013-10-28 12:50
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
462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-28 13:05
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
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-28 13:22
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.
462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-28 13:31
Joel Pearson 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.
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-28 13:47
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"}>
462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-28 13:50
> 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
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-10-28 13:55
You must have required the file at some point or you would get an
uninitialised constant error.
462c96e85144024df16705e088c0d638?d=identicon&s=25 shaik s. (shaik_s)
on 2013-10-28 14:05
Joel Pearson 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.