Strip comments from a string: Difference between revisions
Content added Content deleted
Line 211: | Line 211: | ||
new=apples, pears |
new=apples, pears |
||
</pre> |
|||
=={{header|Ruby}}== |
|||
<lang ruby> |
|||
class String |
|||
def strip_comment( markers = ['#',';'] ) |
|||
re = Regexp.union( markers ) # construct a regular expression which will match any of the markers |
|||
self[0, self =~ re] # slice the string where the regular expression matches, and return it. |
|||
end |
|||
end |
|||
puts 'apples, pears # and bananas'.strip_comment |
|||
str = 'apples, pears ; and bananas' |
|||
puts str.strip_comment |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
apples, pears |
|||
apples, pears |
|||
</pre> |
</pre> |
||