I have a string in Ruby on which I\'m calling the strip method to remove the leading and trailing whitespace. e.g.
s = \"12345 \"
s.strip
H
I'd opt for a solution where s
can never be nil
to start with.
You can use the ||
operator to pass a default value if some_method
returns a falsy value:
s = some_method || '' # default to an empty string on falsy return value
s.strip
Or if s
is already assigned you can use ||=
which does the same thing:
s ||= '' # set s to an empty string if s is falsy
s.strip
Providing default scenario's for the absence of a parameters or variables is a good way to keep your code clean, because you don't have to mix logic with variable checking.