Ka Mok Ka Mok - 2 months ago 8
Ruby Question

Why is there no `.split!` in Ruby?

It just seems pretty logical to have it when there's even a

downcase!
. Has anyone else run into this use case in Ruby?

For the curious, I'm trying to do this:

def some_method(foo)
foo.downcase!.split!(" ")
## do some stuff with foo later. ##
end

some_method("A String like any other")


Instead of this:

def some_method(foo)
foo = foo.downcase.split(" ")
## do some stuff with foo later. ##
end

some_method("A String like any other")


Which isn't a really big deal...but
!
just seems cooler.

Answer

Why is there no .split! in Ruby?

It just seems pretty logical to have it when there's even a downcase!.

It may be logical, but it is impossible: objects cannot change their class or their identity in Ruby. You may be thinking of Smalltalk's become: which doesn't and cannot exist in Ruby. become: changes the identity of an object and thus can also change its class.