[Solved] ruby, undefined method `text’ for nil:NilClass


You have several options

price_element = result.search('span.result-price')[0]
price = price_element ? price_element.text : '$0000'

or

price = (result.search('span.result-price')[0].text rescue '$0000')

from Ruby 2.3.0 you can take advantage of safe navigation operator

price = result.search('span.result-price')[0]&.text || '$0000'

10

solved ruby, undefined method `text’ for nil:NilClass