# How can I use variable as a attribute name

**URL:** <https://www.ruby-forum.com/t/how-can-i-use-variable-as-a-attribute-name/128213>\
**Category:** Ruby\
**Created:** [February 5, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/how-can-i-use-variable-as-a-attribute-name/128213 "2008-02-05T22:23:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddad01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@ddad01](https://www.ruby-forum.com/u/ddad01)\
**Post date:** [February 5, 2008, 10:23pm UTC](https://www.ruby-forum.com/t/how-can-i-use-variable-as-a-attribute-name/128213/1 "2008-02-05T22:23:26Z")

</div>

I’m unable to use the attribute name (variable) to verify the button  
with the attribute value exists.

I’ve tried the following:

#Set Variables  
$attribute\_nm = “type”  
$attribute\_val = “submit”

#Verify Button Exists  
if $ie.button(:$attribute\_nm,$attribute\_val).exists?

#Verify Button Exists  
if $ie.button(:+$attribute\_nm,$attribute\_val).exists?

#Verify Button Exists  
if $ie.button($attribute\_nm,$attribute\_val).exists?

Thanks,

DD

---

<div class="post-metadata">

**Author:** ![ddad01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@ddad01](https://www.ruby-forum.com/u/ddad01)\
**Post date:** [February 6, 2008, 1:10am UTC](https://www.ruby-forum.com/t/how-can-i-use-variable-as-a-attribute-name/128213/2 "2008-02-06T01:10:58Z")

</div>

On Feb 5, 2:23 pm, Darin D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if $ie.button(:$attribute\_nm,$attribute\_val).exists?
> 
> #Verify Button Exists  
> if $ie.button(:+$attribute\_nm,$attribute\_val).exists?
> 
> #Verify Button Exists  
> if $ie.button($attribute\_nm,$attribute\_val).exists?

Wow, that’s a lot of global variables. If it’s a symbol you want:  
irb(main):001:0\> f = “foo”  
=\> “foo”  
irb(main):002:0\> p f.to\_sym, f.intern, :“#{f}”  
:foo  
:foo  
:foo

If it’s a method call you’re trying to do based on the variable, see  
the Object#send method:

## C:\>ri Object#send

## Object#send obj.send(symbol [, args…]) =\> obj obj. **send** (symbol [, args…]) =\> obj

```
 Invokes the method identified by _symbol_, passing it any

```

arguments  
specified. You can use + **send** + if the name +send+ clashes with  
an existing method in _obj_.

```
    class Klass
      def hello(*args)
        "Hello " + args.join(' ')
      end
    end
    k = Klass.new
    k.send :hello, "gentle", "readers" #=> "Hello gentle

```

readers"

---

<div class="post-metadata">

**Author:** ![ddad01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@ddad01](https://www.ruby-forum.com/u/ddad01)\
**Post date:** [February 6, 2008, 12:45am UTC](https://www.ruby-forum.com/t/how-can-i-use-variable-as-a-attribute-name/128213/3 "2008-02-06T00:45:06Z")

</div>

I assume from context that you’re using Watir. I’m pretty sure Watir  
is expecting a symbol there for the first argument, which is probably  
why your attempts aren’t working. Maybe try using the to\_sym method  
of the String class, like this:

if $ie.button($attribute\_nm.to\_sym,$attribute\_val).exists?

Good luck,

wade

---

<div class="post-metadata">

**Author:** ![ddad01](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/919ad9/32.png) [@ddad01](https://www.ruby-forum.com/u/ddad01)\
**Post date:** [February 6, 2008, 5:08pm UTC](https://www.ruby-forum.com/t/how-can-i-use-variable-as-a-attribute-name/128213/4 "2008-02-06T17:08:19Z")

</div>

wade wrote:

> I assume from context that you’re using Watir. I’m pretty sure Watir  
> is expecting a symbol there for the first argument, which is probably  
> why your attempts aren’t working. Maybe try using the to\_sym method  
> of the String class, like this:
> 
> if $ie.button($attribute\_nm.to\_sym,$attribute\_val).exists?
> 
> Good luck,
> 
> wade

Nice! That worked. This will save me tons of extra lines of Code.

Thanks Wade, Your the greatest.

DD
