Flowdock

Notes posted by zoopzoop

RSS feed
July 17, 2009
1 thank

Highlight keywords in a text

Case-insensitive

keywords.inject(text) { |text, keyword| text.gsub(/(#{keyword})/i, "<strong>\\1</strong>") }

<strong> can be replace by whatever HTML tag you want for hightlighting (<b>, <i>, …)

March 2, 2009
2 thanks

Valid options changed in Rails v2.3.0 RC1

The valid options in Rails v2.3.0 RC1 are no longer

:connector and :skip_last_comma, but

:words_connector, :two_words_connector, and :last_word_connector:

>> %w(lorem ipsum dolor sit).to_sentence
=> "lorem, ipsum, dolor, and sit"

>> %w(lorem ipsum dolor sit).to_sentence(:words_connector => ' + ')
=> "lorem + ipsum + dolor, and sit"

>> %w(lorem ipsum).to_sentence(:two_words_connector => ' through ')
=> "lorem through ipsum"

# No effect if more than two words
>> %w(lorem ipsum dolor sit).to_sentence(:two_words_connector => ' through ')
=> "lorem, ipsum, dolor, and sit"

>> %w(lorem ipsum dolor sit).to_sentence(:last_word_connector => ' or ')
=> "lorem, ipsum, dolor or sit"
February 23, 2009
2 thanks

Check if value is included in array of valid values

If you want to check the value of an attribute using an array of valid values, the array has to be defined before the validation, so

validates_inclusion_of :name, :in => VALID_NAMES
VALID_NAMES = %w(Peter Paul Mary)

won’t work, but

VALID_NAMES = %w(Peter Paul Mary)
validates_inclusion_of :name, :in => VALID_NAMES

will.