I sooo bungled the explanation in that bug that I must apologize in
the first sentence. :)
Your patch fixed some problems, but not the one represented by my ether-
sniffing-induced code sample in the bug report. What I meant to put
in that bug was something along the lines of:
h = Hash.new
class << h
def default(k); 2; end
end
p h[12] => 2
I committed a different patch which I thinks solves everything. I
also added a few extra tests too. Give a gander.
I promise to make better bug reports in the future and I at least hope
the making of your patch helped to learn a little more about inner
workings of JRuby's implementation.
-Tom
PS- All explicit method calls (callMethod("foo", args)) to a ruby method
should pass IRubyObject.NULL_ARRAY as an argument. We want to eliminate the
need to null check the args list by always passing in am empty array.
I thought we would assert when this happens, but I guess not.
On Thu, 23 Mar 2006, Marc Hadley defenestrated me:
> 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
--
+ http://www.tc.umn.edu/~enebo +---- mailto:[EMAIL PROTECTED] ----+
| Thomas E Enebo, Protagonist | "Luck favors the prepared |
| | mind." -Louis Pasteur |
-------------------------------------------------------
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