class Object
Public Instance Methods
new_string → str
click to toggle source
Returns another string that may be modified without touching the original
object. This means dup
for a string and to_s
for
any other object.
If a block is given, that may modify a created string (built from a non-string object). For a dup'ed string object the block will not be called.
VALUE rb_obj_new_string( VALUE obj) { VALUE r; r = rb_obj_as_string( obj); if (rb_block_given_p()) rb_yield( r); return r; }
nil_if val → nil or self
click to toggle source
Returns nil
when the string matches val
.
val
is compared using the ===
operator, just like
in the case statement.
"hello".nil_if "NONE" #=> "hello" "NONE".nil_if "NONE" #=> nil "NONE".nil_if /^none$/i #=> nil 20.nil_if 10 #=> 20 10.nil_if 10 #=> nil 1.0.nil_if Float #=> nil
VALUE rb_obj_nil_if( VALUE obj, VALUE val) { if (!id_eqq) id_eqq = rb_intern( "==="); return RTEST( rb_funcall( val, id_eqq, 1, obj)) ? Qnil : obj; }
tap { |x| ... } → obj
click to toggle source
Yields x
to the block, and then returns x
. The
primary purpose of this method is to “tap into” a method chain, in order to
perform operations on intermediate results within the chain.
(1..10) .tap { |x| puts "original: #{x.inspect}" } .to_a .tap { |x| puts "array: #{x.inspect}" } .select { |x| x%2==0 } .tap { |x| puts "evens: #{x.inspect}" } .map { |x| x*x } .tap { |x| puts "squares: #{x.inspect}" }
VALUE rb_krn_tap( VALUE obj) { rb_yield( obj); return obj; }
tap! { |x| ... } → obj
click to toggle source
Yields x
to the block, and then returns x
if and
only if x
is not nil
.
VALUE rb_krn_tap_bang( VALUE obj) { if (!NIL_P( obj)) rb_yield( obj); return obj; }