How can I improve this?
the purpose of this code is to be used in a method that captures a string of hash_tags #twittertype from a form - parse through the list of words and make sure all the words are separated out.
WORD_TEST = "123 sunset #2d2-apple,#home,#star #Babyclub, #apple_surprise #apple,cats mustard#dog , #basic_cable safety #222 #dog-D#DOG#2D "
SECOND_TEST = 'orion#Orion#oRion,Mike'
This is my problem area RegXps...
_string_rgx = /([a-zA-Z0-9]+(-|_)?\w+|#?[a-zA-Z0-9]+(-|_)?\w+)/
add_pound_sign = lambda { |a| a[0].chr == '#' ? a : a='#' + a; a}
I don't know that much Regular Expressions: hence the needed collect the first[element] from the result of the scan -> It yielded weird stuff but the first element was always what I wanted.
t_word = WORD_TEST.scan(_string_rgx).collect {|i| i[0] }
s_word = SECOND_TEST.scan(_string_rgx).collect {|i| i[0] }
t_word.map! { |a| a = add_pound_sign.call(a); a }
s_word.map! { |a| a = add_pound_sign.call(a); a }
The results are what I want. I just want insight from Ruby | Regex guru's out there.
puts t_word.inspect
[
"#123", "#sunset", "#2d2-apple", "#home", "#star", "#Babyclub",
"#apple_surprise", "#apple", "#cats", "#mustard", "#dog",
"#basic_cable", "#safety", "#222", "#dog-D", "#DOG", "#2D"
]
puts s_word.inspect
[
"#orion", "#Orion", "#oRion", "#Mike"
]
Thanks in advance.