Here's a patch to solve bug 1121928. With this patch Hash#default now respects any block you set for the default and supports code like:

h = Hash.new {|h,k| h[k] = k.to_i*10}
puts h[2] # => 20
puts h.default # => 0
puts h.default(2) # => 20

Note that I also had to modify HashMetaClass so Hash#default can accept an argument. I also added a couple of unit tests to test/ testHash.rb and made sure there were no regressions.

Marc.

===================================================================
RCS file: /cvsroot/jruby/jruby/src/org/jruby/RubyHash.java,v
retrieving revision 1.30
diff -r1.30 RubyHash.java
89,90c89,100
<     public IRubyObject getDefaultValue() {
< return (defaultValue == null) ? getRuntime().getNil() : defaultValue;
---
>     public IRubyObject getDefaultValue(IRubyObject[] args) {
>         if (args!=null && args.length>1)
>             throw getRuntime().newArgumentError(args.length, 1);
>
> IRubyObject key = (args==null || args.length==0) ? getRuntime().getNil() : args[0];
>
>         if (!defaultProc.isNil()) {
>             IRubyObject[] procargs = {this, key};
>             return ((RubyProc) defaultProc).call(procargs, this);
>         }
>         else
> return (defaultValue == null) ? getRuntime().getNil () : defaultValue;
234c244
< RubyHash result = newHash(getRuntime(), getValueMap (), getDefaultValue());
---
> RubyHash result = newHash(getRuntime(), getValueMap (), getDefaultValue(null));
276,281c286,287
<         if (!defaultProc.isNil()) {
<               IRubyObject[] args = {this, key};
<               return ((RubyProc) defaultProc).call(args, this);
<         }
<
<         return getDefaultValue();
---
>         IRubyObject[] args = {key};
>         return getDefaultValue(args);
414c420,421
<               return getDefaultValue();
---
>                 IRubyObject[] args = {key};
>               return getDefaultValue(args);

===================================================================
RCS file: /cvsroot/jruby/jruby/src/org/jruby/runtime/builtin/meta/ HashMetaClass.java,v
retrieving revision 1.7
diff -r1.7 HashMetaClass.java
36c36
< defineMethod("default", Arity.noArguments(), "getDefaultValue");
---
> defineMethod("default", Arity.optional(), "getDefaultValue");

===================================================================
RCS file: /cvsroot/jruby/jruby/test/testHash.rb,v
retrieving revision 1.8
diff -r1.8 testHash.rb
39c39
< h = Hash.new {|h,k| h[k] = 10000 }
---
> h = Hash.new {|h,k| h[k] = k.to_i*10 }
41c41,43
< test_equal(10000, h[10])
---
> test_equal(100, h[10])
> test_equal(0, h.default)
> test_equal(20, h.default(2))
44d45
<


---
Marc Hadley <marc.hadley at sun.com>




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Jruby-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jruby-devel

Reply via email to