Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement CAPI function rb_enumeratorize.
- Loading branch information
Showing
8 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
require File.expand_path('../spec_helper', __FILE__) | |||
|
|||
load_extension("enumerator") | |||
|
|||
describe "C-API Enumerator function" do | |||
before :each do | |||
@s = CApiEnumeratorSpecs.new | |||
end | |||
|
|||
describe "rb_enumeratorize" do | |||
before do | |||
@enumerable = [1, 2, 3] | |||
end | |||
|
|||
it "constructs a new Enumerator for the given object, method and arguments" do | |||
enumerator = @s.rb_enumeratorize(@enumerable, :each, :arg1, :arg2) | |||
enumerator.class.should == Enumerable::Enumerator | |||
end | |||
|
|||
it "enumerates the given object" do | |||
enumerator = @s.rb_enumeratorize(@enumerable, :each) | |||
enumerated = [] | |||
enumerator.each { |i| enumerated << i } | |||
enumerated.should == @enumerable | |||
end | |||
|
|||
it "uses the given method for enumeration" do | |||
enumerator = @s.rb_enumeratorize(@enumerable, :awesome_each) | |||
@enumerable.should_receive(:awesome_each) | |||
enumerator.each {} | |||
end | |||
|
|||
it "passes the given arguments to the enumeration method" do | |||
enumerator = @s.rb_enumeratorize(@enumerable, :each, :arg1, :arg2) | |||
@enumerable.should_receive(:each).with(:arg1, :arg2) | |||
enumerator.each {} | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
#include "ruby.h" | |||
#include "rubyspec.h" | |||
|
|||
#ifdef __cplusplus | |||
extern "C" { | |||
#endif | |||
|
|||
#ifdef HAVE_RB_ENUMERATORIZE | |||
VALUE enumerator_spec_rb_enumeratorize(int argc, VALUE *argv, VALUE self) { | |||
VALUE obj, meth, args; | |||
rb_scan_args(argc, argv, "2*", &obj, &meth, &args); | |||
return rb_enumeratorize(obj, meth, RARRAY_LEN(args), RARRAY_PTR(args)); | |||
} | |||
#endif | |||
|
|||
void Init_enumerator_spec() { | |||
VALUE cls; | |||
cls = rb_define_class("CApiEnumeratorSpecs", rb_cObject); | |||
|
|||
#ifdef HAVE_RB_ENUMERATORIZE | |||
rb_define_method(cls, "rb_enumeratorize", enumerator_spec_rb_enumeratorize, -1); | |||
#endif | |||
} | |||
|
|||
#ifdef __cplusplus | |||
} | |||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,20 @@ | |||
#include "capi/capi.hpp" | |||
#include "capi/18/include/ruby.h" | |||
|
|||
using namespace rubinius; | |||
using namespace rubinius::capi; | |||
|
|||
extern "C" { | |||
VALUE rb_enumeratorize(VALUE obj, VALUE meth, int argc, VALUE *argv) { | |||
VALUE* args = (VALUE*)alloca(sizeof(VALUE) * (argc + 2)); | |||
|
|||
args[0] = obj; | |||
args[1] = meth; | |||
|
|||
for(int i = 0; i < argc; i++) { | |||
args[i + 2] = argv[i]; | |||
} | |||
|
|||
return rb_funcall2(rb_cEnumerator, rb_intern("new"), argc + 2, args); | |||
} | |||
} |