You can't assign to self
, which is probably the first thing that comes to mind. However, there's a convenient method String#replace
, which, you know, replaces string's content.
class String
def shuffle
split('').shuffle.join
end
def shuffle!
replace shuffle
end
end
s = 'hello'
s.shuffle!
s # => "lhleo"