Wednesday, 15 January 2014

refactoring - How can I DRY out this Ruby Code -


How can I clear the following Ruby code:

  x = 'A random string If x.include? ('' ') X = x.split (' $ ') [0] if x.include? ('$') 'X = x.split (' ^ ') [0] to become? X = x.split ('*') [0] If x.include? ('*')   

I'm looking for a really elegant Ruby liner, but I'm taking a hard time maybe it's somewhat readable.

Thank you

I think this may be what you are looking for

x.split (/ \ ^ | \ $ | \ * /)

No comments:

Post a Comment