Need to parse a list of checkboxes - ruby mechanize

Hi All,

I have a ruby mechanize form that has a list of some 45 checkboxes. Now
i want to parse that list and then check each checkbox. Any idea how? As
i donot want to write a line of code for each checkbox, i would want to
parse the checkboxes list.
I can get the name of a checkbox using the following code:

chkName = checkboxForm.checkbox.name

Now how would i parse the whole list to store the names in an array.
Once i get those in the array i can use the following code in a for loop
to compare the name in the mechanize checkbox_with(:name =>
'checkbox_name').check

Please let me know what other better approach we can use.

regards.

···

--
Posted via http://www.ruby-forum.com/.

Rochit Sen wrote in post #1102943:

Hi All,

I have a ruby mechanize form that has a list of some 45 checkboxes. Now
i want to parse that list and then check each checkbox. Any idea how? As
i donot want to write a line of code for each checkbox, i would want to
parse the checkboxes list.
I can get the name of a checkbox using the following code:

chkName = checkboxForm.checkbox.name

Now how would i parse the whole list to store the names in an array.
Once i get those in the array i can use the following code in a for loop
to compare the name in the mechanize checkbox_with(:name =>
'checkbox_name').check

Please let me know what other better approach we can use.

regards.

Hi All,

Was able to resolve it by spending some more thought on it :)). Did the
following:

···

------
#Store all the checkbox names in a names array and compare the checkbox
name to check it
allChkBoxes = checkboxForm.checkboxes
chkNames =
allChkBoxes.each do |chkBx|
  chkNames = chkBx.name
  checkboxForm.checkbox_with(:name => "#{chkNames}").check
end
------

--
Posted via http://www.ruby-forum.com/\.

tamouse's suggestion is probably the best way to do this. As you already
have the objects in a collection it's as simple as iterating through it
and performing the same action on each.
If you also want the names then you could do this:

checkboxForm.checkboxes.map{|b| b.click; b.name }

It will click each checkbox and return an array of the names as well.

···

--
Posted via http://www.ruby-forum.com/.

It looks to me like you're trying to set all the checkboxes in a form,
the above seems to be going about it the hard way. Try:

    checkboxForm.checkboxes.map{|b| b.click}

···

On Sat, Mar 23, 2013 at 12:47 PM, Rochit Sen <lists@ruby-forum.com> wrote:

Rochit Sen wrote in post #1102943:

Hi All,

I have a ruby mechanize form that has a list of some 45 checkboxes. Now
i want to parse that list and then check each checkbox. Any idea how? As
i donot want to write a line of code for each checkbox, i would want to
parse the checkboxes list.
I can get the name of a checkbox using the following code:

chkName = checkboxForm.checkbox.name

Now how would i parse the whole list to store the names in an array.
Once i get those in the array i can use the following code in a for loop
to compare the name in the mechanize checkbox_with(:name =>
'checkbox_name').check

Please let me know what other better approach we can use.

regards.

Hi All,

Was able to resolve it by spending some more thought on it :)). Did the
following:
------
#Store all the checkbox names in a names array and compare the checkbox
name to check it
allChkBoxes = checkboxForm.checkboxes
chkNames =
allChkBoxes.each do |chkBx|
  chkNames = chkBx.name
  checkboxForm.checkbox_with(:name => "#{chkNames}").check
end
------

--
Posted via http://www.ruby-forum.com/\.