slice(...)
public
Element Reference—If passed a single Fixnum, returns the code of the character at
that position. If passed two Fixnum
objects, returns a substring starting at the offset given by the first, and
a length given by the second. If given a
range, a substring containing characters at offsets given by the range is
returned. In all three cases, if an offset is negative, it is counted from
the end of str. Returns nil if the initial offset falls
outside the string, the length is
negative, or the beginning of the range is greater than the end.
If a Regexp is supplied, the matching
portion of str is returned. If a numeric parameter follows the
regular expression, that component of the MatchData is returned instead. If a String is given, that string is returned if it
occurs in str. In both cases, nil is returned if there is
no match.
a = "hello there"
a[1]
a[1,3]
a[1..3]
a[-3,2]
a[-4..-2]
a[12..-1]
a[-2..-4]
a[/[aeiou](.)\1/]
a[/[aeiou](.)\1/, 0]
a[/[aeiou](.)\1/, 1]
a[/[aeiou](.)\1/, 2]
a["lo"]
a["bye"]
Show source
/*
* call-seq:
* str[fixnum] => fixnum or nil
* str[fixnum, fixnum] => new_str or nil
* str[range] => new_str or nil
* str[regexp] => new_str or nil
* str[regexp, fixnum] => new_str or nil
* str[other_str] => new_str or nil
* 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(regexp, fixnum) => new_str or nil
* str.slice(other_str) => new_str or nil
*
* Element Reference---If passed a single <code>Fixnum</code>, returns the code
* of the character at that position. If passed two <code>Fixnum</code>
* objects, returns a substring starting at the offset given by the first, and
* a length given by the second. If given a range, a substring containing
* characters at offsets given by the range is returned. In all three cases, if
* an offset is negative, it is counted from the end of <i>str</i>. Returns
* <code>nil</code> if the initial offset falls outside the string, the length
* is negative, or the beginning of the range is greater than the end.
*
* If a <code>Regexp</code> is supplied, the matching portion of <i>str</i> is
* returned. If a numeric parameter follows the regular expression, that
* component of the <code>MatchData</code> is returned instead. If a
* <code>String</code> is given, that string is returned if it occurs in
* <i>str</i>. In both cases, <code>nil</code> is returned if there is no
* match.
*
* a = "hello there"
* a[1] #=> 101
* a[1,3] #=> "ell"
* a[1..3] #=> "ell"
* a[-3,2] #=> "er"
* a[-4..-2] #=> "her"
* a[12..-1] #=> nil
* a[-2..-4] #=> ""
* a[/[aeiou](.)\1/]
* a[/[aeiou](.)\1/, 0]
* a[/[aeiou](.)\1/, 1]
* a[/[aeiou](.)\1/, 2]
* a["lo"]
* a["bye"]
*/
static VALUE
rb_str_aref_m(argc, argv, str)
int argc;
VALUE *argv;
VALUE str;
{
if (argc == 2) {
if (TYPE(argv[0]) == T_REGEXP) {
return rb_str_subpat(str, argv[0], NUM2INT(argv[1]));
}
return rb_str_substr(str, NUM2LONG(argv[0]), NUM2LONG(argv[1]));
}
if (argc != 1) {
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
}
return rb_str_aref(str, argv[0]);
}