Geo has a decent solution, but there's a few missed opportunities there.
If you're only finding one element, it's probably more efficient to use Enumerable#find instead of Enumerable#select and then Array#first on top of that. Alternatively, you can simply do the reassignment during the select.
If you look at the suggested approach there's a chance you'll trigger an exception if a field with that name is not found:
# Original approach
my_fields = form.fields.select {|f| f.name == "whatever"}
# Chance of exception here, calling nil#whatever=
my_fields[1].whatever = "value"
I'd advocate using Enumerable#select and simply doing the work inside the loop, much safer that way:
my_fields = form.fields.select do |f|
if (f.name == "whatever")
# Will only ever trigger if an element is found,
# also works if more than one field has same name.
f.whatever = 'value'
end
end
The other approach is to use Enumerable#find which returns at most one element:
# Finds only a single element
whatever_field = form.fields.find { |f| f.name == "whatever" }
whatever_field and whatever_field.whatever = 'value'
Of course you can always pepper your code with exception catches, but that seems counter-productive.