Range patch

Sorry, I introduced a small bug since I didn’t check all the other specs
were still working (tut tut). The String#upto spec picked up a quirk of
MRI
that I broke in rbx:

This is weird but MRI behaves like that

it “upto calls block with self even if self is less than stop but stop
length is less than self length” do

a = []

"25".upto("5") { |s| a << s }

a.should == ["25"]

end

it “upto doesn’t call block if stop is less than self and stop length
is
less than self length” do

a = []

"25".upto("1") { |s| a << s }

a.should == []

end

This behaviour is also true for Range#each and Range#step too. The fix
is
as follows. If it is a problem I can resubmit the whole patch with this
fix
incorporated.

Inside the RangeOps.cs file, get rid of the CompareString method and
change
StepString to:

    private static Range StepString(CodeContext/*!*/ context, 

Range/!/
self, BlockParam block, MutableString begin, MutableString end, int
step) {

        CheckStep(step);

        MutableString item = begin;

        int comp;

        while((comp = Protocols.Compare(context, item, end)) < 0) {

            if (block.BlockJumped(_BlockSite.Invoke(context, block,

item))) {

                return self;

            }

            for (int i = 0; i < step; ++i) {

                item = (MutableString)RubySites.Successor(context,

item);

            }

            if (item.Length > end.Length) {

                return self;

            }

        }

        if (comp == 0 && !self.ExcludeEnd) {

            _BlockSite.Invoke(context, block, item);

        }

        return self;

    }

Thanks for the catch. Will review and submit ASAP.

Thanks!
-John

From: [email protected]
[mailto:[email protected]] On Behalf Of Peter Bacon
Darwin
Sent: Friday, January 25, 2008 6:53 AM
To: [email protected]
Subject: [Ironruby-core] Range patch

Sorry, I introduced a small bug since I didn’t check all the other specs
were still working (tut tut). The String#upto spec picked up a quirk of
MRI that I broke in rbx:

This is weird but MRI behaves like that

it “upto calls block with self even if self is less than stop but stop
length is less than self length” do
a = []
“25”.upto(“5”) { |s| a << s }
a.should == [“25”]
end

it “upto doesn’t call block if stop is less than self and stop length
is less than self length” do
a = []
“25”.upto(“1”) { |s| a << s }
a.should == []
end

This behaviour is also true for Range#each and Range#step too. The fix
is as follows. If it is a problem I can resubmit the whole patch with
this fix incorporated.

Inside the RangeOps.cs file, get rid of the CompareString method and
change StepString to:

    private static Range StepString(CodeContext/*!*/ context, 

Range/!/ self, BlockParam block, MutableString begin, MutableString
end, int step) {
CheckStep(step);
MutableString item = begin;
int comp;
while((comp = Protocols.Compare(context, item, end)) < 0) {
if (block.BlockJumped(_BlockSite.Invoke(context, block,
item))) {
return self;
}
for (int i = 0; i < step; ++i) {
item = (MutableString)RubySites.Successor(context,
item);
}
if (item.Length > end.Length) {
return self;
}
}
if (comp == 0 && !self.ExcludeEnd) {
_BlockSite.Invoke(context, block, item);
}
return self;
}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs