I am quite new in Ruby so would like to know and learn how to DRY. I have 2 if statement that’s very similar, is there anyway to refactor it?
msg1,msg2 = msg.split('.')
if !msg1.nil?
items = msg1.split(',')
items.each do |item|
item.strip!
end
somefunction(items)
end
if !msg2.nil?
items = msg2.split(',')
items.each do |item|
item.strip!
end
somefunction(items)
end
Looks like you can do away with the two variables:
Edit: I removed the
unlesssince the array created bysplitwill not contain any. In your original code it made sense in case the array had 0 or 1 element, but by usingeachthis becomes unnecessary.