# Can anyone see the obvious msitake I've made?

**URL:** <https://rubytalk.org/t/can-anyone-see-the-obvious-msitake-ive-made/26208>\
**Category:** ruby-talk\
**Created:** [29 March 2006 03:05 UTC](https://rubytalk.org/t/can-anyone-see-the-obvious-msitake-ive-made/26208 "2006-03-29T03:05:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kev\_Jackson2](https://avatars.discourse-cdn.com/v4/letter/k/7ea924/32.png) [@Kev\_Jackson2](https://rubytalk.org/u/Kev_Jackson2)\
**Post date:** [29 March 2006 03:05 UTC](https://rubytalk.org/t/can-anyone-see-the-obvious-msitake-ive-made/26208/1 "2006-03-29T03:05:52Z")

</div>

Hi,

I'm hacking up some scripts and I have a lot of repetitive code ie:

#corporate name  
@@ie.text\_field(:id, @test\_data['pg\_401']['corp\_nm\_fld']).value=@test\_data['pg\_401']['corp\_nm']  
@@ie.text\_field(:id, @test\_data['pg\_401']['corp\_nm\_kn\_fld']).value=@test\_data['pg\_401']['corp\_nm\_kn']  
#rep name  
@@ie.text\_field(:id, @test\_data['pg\_401']['rep\_j\_pos\_fld']).value=@test\_data['pg\_401']['rep\_j\_pos']  
@@ie.text\_field(:id, @test\_data['pg\_401']['rep\_nm\_fld']).value=@test\_data['pg\_401']['rep\_nm']  
@@ie.text\_field(:id, @test\_data['pg\_401']['rep\_nm\_kn\_fld']).value=@test\_data['pg\_401']['rep\_nm\_kn']  
#telephone  
@@ie.text\_field(:id, @test\_data['pg\_401']['tel\_no\_1\_fld']).value=@test\_data['pg\_401']['tel\_no\_1']  
@@ie.text\_field(:id, @test\_data['pg\_401']['tel\_no\_2\_fld']).value=@test\_data['pg\_401']['tel\_no\_2']  
...

It looks like a perfect place to use a macro to write out the call to ie.text\_field at run-time (especially because of the duplicated variables)

I've written a method\_missing method in my base class and tried to implement something like  
@@ie.text\_field(:id, @test\_data['pg\_401']['tel\_no\_1\_fld']).value=@test\_data['pg\_401']['tel\_no\_1'] =\> text :id, :pg\_401, :tel\_no\_1

This would dramatically reduce the amount of typing (and it'll reduce the training time for the qc team who ultimately have to write these scripts), and may allow me to refactor further so that the entire test script could be constructed from a yml file on the fly - which would save a \*lot\* of hassle.

Now I've changed @@ie to $ie as it makes more sense (watir and I only want to be driving one ie window at once, so make it a global)

in my method\_missing definition I have

when /text/=~m.id2name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#works  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ie.text\_field(:id, eval("$test\_data['#{args[1].to\_s}']['#{args[2]}\_fld']").to\_s).set(eval("$test\_data['#{args[1].to\_s}']['#{args[2]}']"))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#doesn't work args[0] = id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ie.text\_field("#{args[0].to\_sym}", eval("$test\_data['#{args[1].to\_s}']['#{args[2]}\_fld']")).set(eval("$test\_data['#{args[1].to\_s}']['#{args[2]}']"))

What I want to do is to create a call to $ie.text\_field with the correct params - as you can see if I specify the symbol :id, it works perfectly (not sure eval is the correct thing to use but it works), however if I try to get the symbol from the args[] and then to\_sym before executing, I always get

testScenario(NewApplicationTest):  
Watir::Exception::UnknownObjectException: Unable to locate object, using id and  
form:address1

However I've just tried (as I was typing this email)  
$ie.text\_field(eval("args[0].to\_sym"), eval("$test\_data['#{args[1].to\_s}']['#{args[2]}\_fld']")).set(eval("$test\_data['#{args[1].to\_s}']['#{args[2]}']"))

And it works! ☹

Can anyone explain why #{args[0].to\_sym} is not actually evaluating to the :id symbol, but eval(args[0].to\_sym) does? Especially as the error produced from watir is exactly the same as if I'd have typed:

$ie.text\_field(:id, 'a\_field\_that\_doesn't\_exist')

Also if there is a better way to do this without using three eval calls per method that I want to add (seems a little overkill to me)

Thanks  
Kev

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [29 March 2006 03:30 UTC](https://rubytalk.org/t/can-anyone-see-the-obvious-msitake-ive-made/26208/2 "2006-03-29T03:30:54Z")

</div>

Hi --

> **···**
>
> On Wed, 29 Mar 2006, Kev Jackson wrote:
> 
> > Can anyone explain why #{args[0].to\_sym} is not actually evaluating to the :id symbol, but eval(args[0].to\_sym) does? Especially as the error produced from watir is exactly the same as if I'd have typed:
> > 
> > $ie.text\_field(:id, 'a\_field\_that\_doesn't\_exist')
> 
> I'm not sure about that part, but I think the problem probably stems  
> from the fact that "#{'sym'.to\_sym}" is a string. Anything inside the  
> #{...} construct gets to\_s called on it, which trumps the internal  
> to\_sym call.
> 
> David
> 
> --  
> David A. Black (dblack@wobblini.net)  
> Ruby Power and Light, LLC ([http://www.rubypowerandlight.com](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" chapters now available  
> from Manning Early Access Program! [Ruby for Rails](http://www.manning.com/books/black)

---

<div class="post-metadata">

**Author:** ![Dave\_Burt2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/dave_burt2/32/2036_2.png) [@Dave\_Burt2](https://rubytalk.org/u/Dave_Burt2)\
**Post date:** [29 March 2006 05:48 UTC](https://rubytalk.org/t/can-anyone-see-the-obvious-msitake-ive-made/26208/3 "2006-03-29T05:48:42Z")

</div>

Kev Jackson wrote:

> Can anyone see the obvious msitake I've made?

It's "msitake" - should be "mistake" 😉

No, really...

> ...  
> @@ie.text\_field(:id,  
> @test\_data['pg\_401']['tel\_no\_2\_fld']).value=@test\_data['pg\_401']['tel\_no\_2']  
> ..
> 
> It looks like a perfect place to use a macro to write out the call to  
> ie.text\_field at run-time (especially because of the duplicated variables)
> 
> I've written a method\_missing method in my base class and tried to  
> implement something like  
> @@ie.text\_field(:id,  
> @test\_data['pg\_401']['tel\_no\_1\_fld']).value=@test\_data['pg\_401']['tel\_no\_1']  
> =\> text :id, :pg\_401, :tel\_no\_1  
> ...  
> Now I've changed @@ie to $ie as it makes more sense (watir and I only want  
> to be driving one ie window at once, so make it a global)

This is the obvious solution to me:

# call: text "pg\_401", "tel\_no\_1"  
def text(test\_id, field\_name)  
&nbsp;&nbsp;$ie.text\_field(:id, $test\_data[test\_id][field\_name + '\_fld']).value =  
&nbsp;&nbsp;&nbsp;&nbsp;$test\_data[test\_id][field\_name]  
end

> in my method\_missing definition I have
> 
> when /text/=~m.id2name

That will match any method call with "text" in the name. Why do you want  
that? Just define the method you want ("text").

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#works  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ie.text\_field(:id,  
> eval("$test\_data['#{args[1].to\_s}']['#{args[2]}\_fld']").to\_s).set(eval("$test\_data['#{args[1].to\_s}']['#{args[2]}']"))

You should not need eval to do this. Not at all. The above line is the same  
as:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ie.text\_field(:id, ($test\_data[args[1].to\_s][args[2].to\_s +  
'\_fld']).to\_s).set(  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$test\_data[args[1].to\_s][args[2].to\_s])

Which you can simplify to this if you assume args[1] and args[2] are  
strings:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ie.text\_field(:id, $test\_data[args[1]][args[2] +  
'\_fld']).set($test\_data[args[1]][args[2]])

Summary: less string interpolation, less to\_s, and no eval. Keep it simple,  
just deal with variables. You're just making it needlessly complicated and  
confusing yourself.

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#doesn't work args[0] = id  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$ie.text\_field("#{args[0].to\_sym}",  
> eval("$test\_data['#{args[1].to\_s}']['#{args[2]}\_fld']")).set(eval("$test\_data['#{args[1].to\_s}']['#{args[2]}']"))

Let's look at this: "#{args[0].to\_sym}"

args[0] is being cast to Symbol, then back to String because it's being  
interpolated into one.

Hint: "#{x}" is just a strange way of writing x.to\_s. If you expect x to be  
a string, it's the same as just plain old x. "Anything in between quotes"  
will always give you a string.

So, removing the evals and string interpolations, we get:  
&nbsp;&nbsp;$ie.text\_field(args[0].to\_sym.to\_s, $test\_data[args[1].to\_s][args[2].to\_s  
+ '\_fld']).set(  
&nbsp;&nbsp;&nbsp;&nbsp;$test\_data[args[1].to\_s][args[2].to\_s])

And then we can remove all the to\_s: the first one is an error (the obvious  
mistake you're trying to locate), and the rest are unnecessary if we assume  
we're being passed a string:  
&nbsp;&nbsp;$ie.text\_field(args[0].to\_sym, $test\_data[args[1]][args[2] +  
'\_fld']).set(  
&nbsp;&nbsp;&nbsp;&nbsp;$test\_data[args[1]][args[2]])

A little easier on the eyes?

> What I want to do is to create a call to $ie.text\_field with the correct  
> params - as you can see if I specify the symbol :id, it works perfectly  
> (not sure eval is the correct thing to use but it works), however if I try  
> to get the symbol from the args and then to\_sym before executing, I  
> always get
> 
> testScenario(NewApplicationTest):  
> Watir::Exception::UnknownObjectException: Unable to locate object, using  
> id and  
> form:address1

I would expect Watir to be a little more helpful by either accepting the  
"id" that you're passing, or telling you that it doesn't know how to locate  
objects using a string rather than the symbol it's expecting. I'll ask Bret  
about improving this.

(As I explained earlier in this message, your "to\_sym" was interpolated into  
a string, so you were sending a string to text\_field.)

> However I've just tried (as I was typing this email)  
> $ie.text\_field(eval("args[0].to\_sym"),  
> eval("$test\_data['#{args[1].to\_s}']['#{args[2]}\_fld']")).set(eval("$test\_data['#{args[1].to\_s}']['#{args[2]}']"))
> 
> And it works! ☹
> 
> Can anyone explain why #{args[0].to\_sym} is not actually evaluating to the  
> :id symbol, but eval(args[0].to\_sym) does? Especially as the error  
> produced from watir is exactly the same as if I'd have typed:

"#{1 + 1}" is the same as (1 + 1).to\_s, as I explained earlier.  
eval("1 + 1") is the same as (1 + 1).

Cheers,  
Dave
