Inline symbols

Hello,

I'm fairly new to Ruby, cucumber, and WebDriver and ran into what seems to be a Ruby issue. It doesn't seem to be handling my .intern or .to_sym in this mock-up inline form, ele.send_keys(x[i], x[i].intern, x[i], x[i].intern)

I tried both intern and to_sym with the same result, as expected.

I'm trying to parse a string and output a string with symbol parameter.

Input string: 'Student One', :return, 'Student Two', :return Output: ele.send_keys('Student One', :return, 'Student Two', :return)

def fill_in(field_name,text) # ele = Find.Element(find_locator(:text_field,field_name)) # ele.clear #The above correctly obtains the text: # 'Student One', :return, 'Student Two', :return #Therefore, hard code to test:    text = "'Student One', :return, 'Student Two', :return"    items = text.split(/,\s?/)    ele.send_keys(items.each_index {|x|      next if x % 2 == 1      if (x<items.size-1) then items +', ' else items end     items[x+1].intern     }) end

Output 'Student One', :return, 'Student Two', :return

When I run this instead of the symbols acting like symbols they act like strings, the :returns are not resolved.

However, hard coded strings and symbols like the following work as expected: ele.send_keys('Student One', :return, 'Student Two', :return)

Output Student One Student Two

References: Send Keys syntax http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Element.html#send_keys-instance_method

Keys you can send: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Keys.html

Couple problems here:

- each_index returns the original array. You're not assigning any new values to it, so of course .intern isn't having any effect.

- not sure exactly what you're doing with the +', ' there, but I suspect you really want Ruby's "splat" operator. It takes an array and fills in the arguments to the method with each element (simplified description). Your code would end up looking like:

   items = text.split(/,\s?/)    ele.send_keys(*items.map {|x|      case x      when /^'(.*)'$/ then $1 # "'Student One'" => "Student One"      when /^:(.*)$/ then $1.to_sym # ":return" => :return      end     })

--Matt Jones

From your comment it looks like I may need to understand why I would need to assign values return is already part of the text so my assumption is that I just have to .to_sym it. However, if I have to assign and deliver something that would change quite a bit.

Clarification.

The desired resulting line is: ele.send_keys(‘Student One’, :return, ‘Student Two’, :return) Basically “‘Student One’, :return, ‘Student Two’, :return” is parsed to result in itself except with the symbols understood as symbol type.

In the original: x[0] == ‘Student One’ ', ’ is added since it is not the last element x[1] == ‘return’ output as x[1].to_sym which adds the : itself. ', ’ is added since it is not the last element

Pattern repeats.

Mat, your idea has made the :return do what it is supposed to but now I’m unable to get the text to output. All that is coming out is the symbols even though puts on the text line shows it should be displaying in the following: (I added .to_s even though I shouldn’t have to just to be sure the type hadn’t changed.)

items = text.split(/,\s?/)
      ele.send_keys(*items.map {|x|
      case x
        when /^.*'.*/ then $1.to_s+', '; puts 'hit a quote' # "'Student One'" => "Student One" (puts shows is hit but text not displaying on screen)

        when /^:(.*)$/ then $1.to_sym # ":return" => :return (outputting but requires comma?)
	else $1.to_s #return unknowns as is (nothing should hit this)
      end
    })

Is it because the commas are stripped? I tried adding them back in with + ’ , ’ on the to_sym line but received the error, ‘undefined method `+’ for :return:Symbol’

I also am unsure how the case structure can detect if the element is ‘last’ so that it doesn’t add the comma on the last element.

From your comment it looks like I may need to understand why I would need to assign values return is already part of the text so my assumption is that I just have to .to_sym it. However, if I have to assign and deliver something that would change quite a bit.

Calling .to_sym on a string returns a NEW symbol. It does exactly nothing to the string you called it on.

Clarification.

The desired resulting line is: ele.send_keys('Student One', :return, 'Student Two', :return) Basically "'Student One', :return, 'Student Two', :return" is parsed to result in itself except with the symbols understood as symbol type.

In the original: x[0] == 'Student One' ', ' is added since it is not the last element x[1] == 'return' output as x[1].to_sym which adds the : itself. ', ' is added since it is not the last element

Pattern repeats.

No. You don't need to put commas in when you're splatting an array. An example:

def foo(arg1, arg2, arg3)   puts "arg1 is #{arg1.inspect}"   puts "arg2 is #{arg2.inspect}"   puts "arg3 is #{arg3.inspect}" end

a = [1,2,3]

Then calling:

foo(*a)

will result in: arg1 is 1 arg2 is 2 arg3 is 3

Mat, your idea has made the :return do what it is supposed to but now I'm unable to get the text to output. All that is coming out is the symbols even though puts on the text line shows it should be displaying in the following: (I added .to_s even though I shouldn't have to just to be sure the type hadn't changed.)

 items = text\.split\(/,\\s?/\)
  ele\.send\_keys\(\*items\.map \{|x|
  case x
    when /^\.\*&#39;\.\*/ then $1\.to\_s\+&#39;, &#39;

$1 is not being set here - it's used to capture the first set of parentheses in a regular expression. Your regex is missing them.

    when /^:\(\.\*\)$/ then $1\.to\_sym    \# &quot;:return&quot;       =&gt; :return
    else $1\.to\_s                                   \#return unknowns as is \(nothing should hit this\)

Again, $1 will not be set here. x.to_s would be a suitable default.

I also am unsure how the case structure can detect if the element is 'last' so that it doesn't add the comma on the last element.

No commas needed. :slight_smile:

I highly recommend you check out Programming Ruby (aka "The Pickaxe") - or at least the first edition of it, available free here:

http://www.ruby-doc.org/docs/ProgrammingRuby/

--Matt Jones