Skip to content

Instantly share code, notes, and snippets.

@obfuscurity
Created July 21, 2011 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obfuscurity/1096433 to your computer and use it in GitHub Desktop.
Save obfuscurity/1096433 to your computer and use it in GitHub Desktop.
Goodbye irb, hello pry.
pry(main)> @foo = []
=> []
pry(main)> show-doc @foo.index
From: array.c in Ruby Core (C Method):
Number of lines: 14
signature: index(*)
Returns the index of the first object in self such that is
== to obj. If a block is given instead of an
argument, returns first object for which block is true.
Returns nil if no match is found.
See also Array#rindex.
If neither block nor argument is given, an enumerator is returned instead.
a = [ "a", "b", "c" ]
a.index("b") #=> 1
a.index("z") #=> nil
a.index{|x|x=="b"} #=> 1
This is an alias of #find_index.
pry(main)> show-method @foo.index
From: array.c in Ruby Core (C Method):
Number of lines: 24
static VALUE
rb_ary_index(int argc, VALUE *argv, VALUE ary)
{
VALUE val;
long i;
if (argc == 0) {
RETURN_ENUMERATOR(ary, 0, 0);
for (i=0; i<RARRAY_LEN(ary); i++) {
if (RTEST(rb_yield(RARRAY_PTR(ary)[i]))) {
return LONG2NUM(i);
}
}
return Qnil;
}
rb_scan_args(argc, argv, "1", &val);
if (rb_block_given_p())
rb_warn("given block not used");
for (i=0; i<RARRAY_LEN(ary); i++) {
if (rb_equal(RARRAY_PTR(ary)[i], val))
return LONG2NUM(i);
}
return Qnil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment