David said:
The ! doesn't mean that a method modifies the receiver in place,
though, but rather that this is a "dangerous" version of the method
and should be used carefully. The "danger" often has to do with
in-place change, but ! itself doesn't mean that.
True, but the large majority of ! methods are simply in-place
equivalents of their non-bang friends:
---Instance
methods------------------------------------------------------------
Hash#merge!
hsh.merge!(other_hash) => hsh
hsh.update(other_hash) => hsh
hsh.merge!(other_hash){|key, oldval, newval| block} => hsh
hsh.update(other_hash){|key, oldval, newval| block} => hsh
------------------------------------------------------------------------
Adds the contents of _other_hash_ to _hsh_, overwriting entries
with duplicate keys with those from _other_hash_.
h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 254, "c" => 300 }
h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
----------------------------------------------------------- Hash#reject!
hsh.reject! {| key, value | block } -> hsh or nil
------------------------------------------------------------------------
Equivalent to +Hash#delete_if+, but returns +nil+ if no changes
were made.
------------------------------------------------------------ Array#uniq!
array.uniq! -> array or nil
------------------------------------------------------------------------
Removes duplicate elements from _self_. Returns +nil+ if no changes
are made (that is, no duplicates are found).
a = [ "a", "a", "b", "b", "c" ]
a.uniq! #=> ["a", "b", "c"]
b = [ "a", "b", "c" ]
b.uniq! #=> nil
----------------------------------------------------------- Array#slice!
array.slice!(index) -> obj or nil
array.slice!(start, length) -> sub_array or nil
array.slice!(range) -> sub_array or nil
------------------------------------------------------------------------
Deletes the element(s) given by an index (optionally with a length)
or by a range. Returns the deleted object, subarray, or +nil+ if
the index is out of range. Equivalent to:
def slice!(*args)
result = self[*args]
self[*args] = nil
result
end
a = [ "a", "b", "c" ]
a.slice!(1) #=> "b"
a #=> ["a", "c"]
a.slice!(-1) #=> "c"
a #=> ["a"]
a.slice!(100) #=> nil
a #=> ["a"]
------------------------------------------------------------ Array#sort!
array.sort! -> array
array.sort! {| a,b | block } -> array
------------------------------------------------------------------------
Sorts _self_. Comparisons for the sort will be done using the +<=>+
operator or using an optional code block. The block implements a
comparison between _a_ and _b_, returning -1, 0, or +1. See also
+Enumerable#sort_by+.
a = [ "d", "a", "e", "c", "b" ]
a.sort #=> ["a", "b", "c", "d", "e"]
a.sort {|x,y| y <=> x } #=> ["e", "d", "c", "b", "a"]
--------------------------------------------------------- Array#collect!
array.collect! {|item| block } -> array
array.map! {|item| block } -> array
------------------------------------------------------------------------
Invokes the block once for each element of _self_, replacing the
element with the value returned by _block_. See also
+Enumerable#collect+.
a = [ "a", "b", "c", "d" ]
a.collect! {|x| x + "!" }
a #=> [ "a!", "b!", "c!", "d!" ]
--------------------------------------------------------- Array#compact!
array.compact! -> array or nil
------------------------------------------------------------------------
Removes +nil+ elements from array. Returns +nil+ if no changes were
made.
[ "a", nil, "b", nil, "c" ].compact! #=> [ "a", "b", "c" ]
[ "a", "b", "c" ].compact! #=> nil
---------------------------------------------------------- Array#reject!
array.reject! {|item| block } -> array or nil
------------------------------------------------------------------------
Equivalent to +Array#delete_if+, deleting elements from _self_ for
which the block evaluates to true, but returns +nil+ if no changes
were made. Also see +Enumerable#reject+.
--------------------------------------------------------- Array#reverse!
array.reverse! -> array
------------------------------------------------------------------------
Reverses _self_ in place.
a = [ "a", "b", "c" ]
a.reverse! #=> ["c", "b", "a"]
a #=> ["c", "b", "a"]
------------------------------------------------------------- Array#map!
array.collect! {|item| block } -> array
array.map! {|item| block } -> array
------------------------------------------------------------------------
Invokes the block once for each element of _self_, replacing the
element with the value returned by _block_. See also
+Enumerable#collect+.
a = [ "a", "b", "c", "d" ]
a.collect! {|x| x + "!" }
a #=> [ "a!", "b!", "c!", "d!" ]
--------------------------------------------------------- Array#flatten!
array.flatten! -> array or nil
------------------------------------------------------------------------
Flattens _self_ in place. Returns +nil+ if no modifications were
made (i.e., _array_ contains no subarrays.)
a = [ 1, 2, [3, [4, 5] ] ]
a.flatten! #=> [1, 2, 3, 4, 5]
a.flatten! #=> nil
a #=> [1, 2, 3, 4, 5]
----------------------------------------------------------- String#gsub!
str.gsub!(pattern, replacement) => str or nil
str.gsub!(pattern) {|match| block } => str or nil
------------------------------------------------------------------------
Performs the substitutions of +String#gsub+ in place, returning
_str_, or +nil+ if no substitutions were performed.
----------------------------------------------------------- String#succ!
str.succ! => str
str.next! => str
------------------------------------------------------------------------
Equivalent to +String#succ+, but modifies the receiver in place.
------------------------------------------------------- String#downcase!
str.downcase! => str or nil
------------------------------------------------------------------------
Downcases the contents of _str_, returning +nil+ if no changes were
made.
-------------------------------------------------------- String#squeeze!
str.squeeze!([other_str]*) => str or nil
------------------------------------------------------------------------
Squeezes _str_ in place, returning either _str_, or +nil+ if no
changes were made.
--------------------------------------------------------- String#rstrip!
str.rstrip! => self or nil
------------------------------------------------------------------------
Removes trailing whitespace from _str_, returning +nil+ if no
change was made. See also +String#lstrip!+ and +String#strip!+.
" hello ".rstrip #=> " hello"
"hello".rstrip! #=> nil
---------------------------------------------------------- String#slice!
str.slice!(fixnum) => fixnum or nil
str.slice!(fixnum, fixnum) => new_str or nil
str.slice!(range) => new_str or nil
str.slice!(regexp) => new_str or nil
str.slice!(other_str) => new_str or nil
------------------------------------------------------------------------
Deletes the specified portion from _str_, and returns the portion
deleted. The forms that take a +Fixnum+ will raise an +IndexError+
if the value is out of range; the +Range+ form will raise a
+RangeError+, and the +Regexp+ and +String+ forms will silently
ignore the assignment.
string = "this is a string"
string.slice!(2) #=> 105
string.slice!(3..6) #=> " is "
string.slice!(/s.*t/) #=> "sa st"
string.slice!("r") #=> "r"
string #=> "thing"
----------------------------------------------------------- String#chop!
str.chop! => str or nil
------------------------------------------------------------------------
Processes _str_ as for +String#chop+, returning _str_, or +nil+ if
_str_ is the empty string. See also +String#chomp!+.
----------------------------------------------------- String#capitalize!
str.capitalize! => str or nil
------------------------------------------------------------------------
Modifies _str_ by converting the first character to uppercase and
the remainder to lowercase. Returns +nil+ if no changes are made.
a = "hello"
a.capitalize! #=> "Hello"
a #=> "Hello"
a.capitalize! #=> nil
------------------------------------------------------------- String#tr!
str.tr!(from_str, to_str) => str or nil
------------------------------------------------------------------------
Translates _str_ in place, using the same rules as +String#tr+.
Returns _str_, or +nil+ if no changes were made.
----------------------------------------------------------- String#next!
str.succ! => str
str.next! => str
------------------------------------------------------------------------
Equivalent to +String#succ+, but modifies the receiver in place.
---------------------------------------------------------- String#chomp!
str.chomp!(separator=$/) => str or nil
------------------------------------------------------------------------
Modifies _str_ in place as described for +String#chomp+, returning
_str_, or +nil+ if no modifications were made.
------------------------------------------------------- String#swapcase!
str.swapcase! => str or nil
------------------------------------------------------------------------
Equivalent to +String#swapcase+, but modifies the receiver in
place, returning _str_, or +nil+ if no changes were made.
-------------------------------------------------------- String#reverse!
str.reverse! => str
------------------------------------------------------------------------
Reverses _str_ in place.
----------------------------------------------------------- String#tr_s!
str.tr_s!(from_str, to_str) => str or nil
------------------------------------------------------------------------
Performs +String#tr_s+ processing on _str_ in place, returning
_str_, or +nil+ if no changes were made.
---------------------------------------------------------- String#strip!
str.strip! => str or nil
------------------------------------------------------------------------
Removes leading and trailing whitespace from _str_. Returns +nil+
if _str_ was not altered.
------------------------------------------------------------ String#sub!
str.sub!(pattern, replacement) => str or nil
str.sub!(pattern) {|match| block } => str or nil
------------------------------------------------------------------------
Performs the substitutions of +String#sub+ in place, returning
_str_, or +nil+ if no substitutions were performed.
--------------------------------------------------------- String#upcase!
str.upcase! => str or nil
------------------------------------------------------------------------
Upcases the contents of _str_, returning +nil+ if no changes were
made.
--------------------------------------------------------- String#delete!
str.delete!([other_str]+>) => str or nil
------------------------------------------------------------------------
Performs a +delete+ operation in place, returning _str_, or +nil+
if _str_ was not modified.
--------------------------------------------------------- String#lstrip!
str.lstrip! => self or nil
------------------------------------------------------------------------
Removes leading whitespace from _str_, returning +nil+ if no change
was made. See also +String#rstrip!+ and +String#strip!+.
" hello ".lstrip #=> "hello "
"hello".lstrip! #=> nil
---Singleton
methods---------------------------------------------------------
Process::exit!
Process.exit!(fixnum=-1)
------------------------------------------------------------------------
Exits the process immediately. No exit handlers are run. _fixnum_
is returned to the underlying system as the exit status.
Process.exit!(0)
Nonetheless, I don't care about select!. Someone made a comment, err,
somewhere, about how all the stupid shit gets bantered to death simply
because more people understand it. It might have been Jason Hunter (of
JDOM fame). Not sure. It was funny, though.
Devin
And yes, it's merely a coincidence that the exception to the rule is the
last one in this list...