Count words

Two questions:

-How can I count words in a variable string?
and
-I have a variable, such a, string=“INN SO”. How can I found this string
in mysql database where the field is “DINNER, SOAP”, for exemple?

Thank you,

Diego

On 5/28/07, Diego Muñoz [email protected] wrote:

Two questions:

-How can I count words in a variable string?

“How can I count words in a variable string?”.split(/\s+/).length # => 9

and

-I have a variable, such a, string=“INN SO”. How can I found this string
in mysql database where the field is “DINNER, SOAP”, for exemple?

search_params = “INN SO”
search_conditions = search_params.split(/\s+/).map{|s| “name LIKE
‘%#{s}%’”
}.join(" AND ") # => “name LIKE ‘%INN%’ AND name LIKE ‘%SO%’”

Product.find(:all, :conditions => search_conditions)


Scott B.
Electro Interactive, Inc.
Office: 813-333-5508
http://ElectroInteractive.com http://electrointeractive.com/
http://sbecker.net

Scott B. wrote:

On 5/28/07, Diego Muñoz [email protected] wrote:

-I have a variable, such a, string=“INN SO”. How can I found this string
in mysql database where the field is “DINNER, SOAP”, for exemple?

search_params = “INN SO”
search_conditions = search_params.split(/\s+/).map{|s| “name LIKE
‘%#{s}%’”
}.join(" AND ") # => “name LIKE ‘%INN%’ AND name LIKE ‘%SO%’”

Product.find(:all, :conditions => search_conditions)

Thanks Scott for your answer, but the solution for my question is in
http://www.ruby-forum.com/topic/109645#new

Diego

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs