DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Add An Array Of Hash Tags To A String
This method adds an array of hash tags to a given string, as many as possible, in order. It returns a new string of up to 140 characters, unless the original string was 140 characters or longer, in which case the original string is returned. Useful for automated Twitter status updates.
def hash_tag(str, tags)
while tags && str.length < 140 && tags.length > 0
tag = tags.shift
if (str.length + tag.length) < 139
str << " \##{tag}"
end
end
str
end
Example: A short string and a few short tags.
hash_tag('A short string.', ['tag1', 'tag2', 'tag3'])
=> "A short string. #tag1 #tag2 #tag3"





