Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Extending Hast class with custom [] []= methods
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Iñaki Baz Castillo  
View profile  
 More options Apr 22 2008, 7:30 pm
Newsgroups: comp.lang.ruby
From: Iñaki Baz Castillo <i...@aliax.net>
Date: Tue, 22 Apr 2008 18:30:34 -0500
Local: Tues, Apr 22 2008 7:30 pm
Subject: Extending Hast class with custom [] []= methods
Hi, I'd like to extend Hash class [] and []= methods in order to find a key
with case insensitive. This is:

- The actual Hash [] behaviour:

{"a"=>1,"b"=>2}["a"]
=> 1
{"a"=>1,"b"=>2}["A"]
=> nil

- The beaviour I look for:

{"a"=>1,"b"=>2}["a"]
=> 1
{"a"=>1,"b"=>2}["A"]
=> 1

But I can't modify [] method since it's Ruby core written in C:

----------------------
VALUE
rb_hash_aref(hash, key)
    VALUE hash, key;
{
    VALUE val;

    if (!st_lookup(RHASH(hash)->tbl, key, &val)) {
        return rb_funcall(hash, id_default, 1, key);
    }
    return val;

}

-----------------------

How could I do it?

--
Iñaki Baz Castillo


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Iñaki Baz Castillo  
View profile  
 More options Apr 22 2008, 8:01 pm
Newsgroups: comp.lang.ruby
From: Iñaki Baz Castillo <i...@aliax.net>
Date: Tue, 22 Apr 2008 19:01:59 -0500
Local: Tues, Apr 22 2008 8:01 pm
Subject: Re: Extending Hast class with custom [] []= methods
El Miércoles, 23 de Abril de 2008, David A. Black escribió:

> A better way is to
> write a module, and then use it selectively for the hashes that need
> it:

>    module CaseInsensitiveLookup
>      def [](key)

Yes, but my question is what to do into that:
  def [](key)" method
    ...
  end

since the original code is written in C and I don't know which attributes
should I use to access to keys and values.

--
Iñaki Baz Castillo


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Finnie  
View profile  
 More options Apr 22 2008, 8:15 pm
Newsgroups: comp.lang.ruby
From: Daniel Finnie <d...@danfinnie.com>
Date: Tue, 22 Apr 2008 19:15:37 -0500
Local: Tues, Apr 22 2008 8:15 pm
Subject: Re: Extending Hast class with custom [] []= methods
Hi,

You can use super and/or alias:

class MyHash < Hash
  def [] key
    super(key.downcase)
  end
end

Dan

On Tue, Apr 22, 2008 at 8:01 PM, Iñaki Baz Castillo <i...@aliax.net> wrote:


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Iñaki Baz Castillo  
View profile  
 More options Apr 22 2008, 9:02 pm
Newsgroups: comp.lang.ruby
From: Iñaki Baz Castillo <i...@aliax.net>
Date: Tue, 22 Apr 2008 20:02:06 -0500
Local: Tues, Apr 22 2008 9:02 pm
Subject: Re: Extending Hast class with custom [] []= methods
El Miércoles, 23 de Abril de 2008, Daniel Finnie escribió:

> Hi,

> You can use super and/or alias:

> class MyHash < Hash
>   def [] key
>     super(key.downcase)
>   end
> end

> Dan

opss, yes, it was no so difficult XDD
Thanks a lot.

--
Iñaki Baz Castillo


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Shea  
View profile  
 More options Apr 22 2008, 9:31 pm
Newsgroups: comp.lang.ruby
From: Chris Shea <cms...@gmail.com>
Date: Tue, 22 Apr 2008 18:31:07 -0700 (PDT)
Local: Tues, Apr 22 2008 9:31 pm
Subject: Re: Extending Hast class with custom [] []= methods
On Apr 22, 6:15 pm, Daniel Finnie <d...@danfinnie.com> wrote:

> Hi,

> You can use super and/or alias:

> class MyHash < Hash
>   def [] key
>     super(key.downcase)
>   end
> end

But he asking for case-insensitivity.  If a key is created with
uppercase letters, you're out of luck.  And if you look for a value
for a non-string key, that's no good either:

###
class MyHash < Hash
  def [](key)
    super(key.downcase)
  end
end

h = MyHash.new
h['A'] = 'never findable'
h['A'] # => nil
h[1]   # ~> undefined method `downcase' for 1:Fixnum (NoMethodError)
###

You could override []= as well (and with more care), but I wonder if a
different class with a Hash instance variable with mediated access
would be a better route.

Chris


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Shea  
View profile  
 More options Apr 22 2008, 10:54 pm
Newsgroups: comp.lang.ruby
From: Chris Shea <cms...@gmail.com>
Date: Tue, 22 Apr 2008 19:54:39 -0700 (PDT)
Local: Tues, Apr 22 2008 10:54 pm
Subject: Re: Extending Hast class with custom [] []= methods
On Apr 22, 7:31 pm, Chris Shea <cms...@gmail.com> wrote:

Or just use Rubinius: http://pastie.textmate.org/185232

!!!
Chris


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Apr 23 2008, 3:22 am
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Wed, 23 Apr 2008 02:22:59 -0500
Local: Wed, Apr 23 2008 3:22 am
Subject: Re: Extending Hast class with custom [] []= methods
2008/4/23, Chris Shea <cms...@gmail.com>:

> Or just use Rubinius: http://pastie.textmate.org/185232

Or delegation

h = CiHash.new
h["FOO"]=1
h[:not_a_string]=2 # works, too
puts h["foo"]
puts h[:not_a_string]

Cheers

robert

--
use.inject do |as, often| as.you_can - without end


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Apr 23 2008, 3:23 am
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Wed, 23 Apr 2008 02:23:35 -0500
Local: Wed, Apr 23 2008 3:23 am
Subject: Re: Extending Hast class with custom [] []= methods
2008/4/23, Robert Klemme <shortcut...@googlemail.com>:

> 2008/4/23, Chris Shea <cms...@gmail.com>:

> > Or just use Rubinius: http://pastie.textmate.org/185232

> Or delegation

Copy and paste error: this was missing:

require 'delegate'

class CiHash < DelegateClass(Hash)
  def initialize
    super({})
  end

  def []=(k,v)
    __getobj__[(k.downcase rescue k)] = v
  end

  def [](k)
    __getobj__[(k.downcase rescue k)]
  end

  # add other lookup and mutation methods
end

>  h = CiHash.new
>  h["FOO"]=1
>  h[:not_a_string]=2 # works, too
>  puts h["foo"]
>  puts h[:not_a_string]

>  Cheers

>  robert

--
use.inject do |as, often| as.you_can - without end

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Iñaki Baz Castillo  
View profile  
 More options Apr 23 2008, 4:02 am
Newsgroups: comp.lang.ruby
From: Iñaki Baz Castillo <i...@aliax.net>
Date: Wed, 23 Apr 2008 03:02:29 -0500
Local: Wed, Apr 23 2008 4:02 am
Subject: Re: Extending Hast class with custom [] []= methods
2008/4/23, Chris Shea <cms...@gmail.com>:

> But he asking for case-insensitivity.  If a key is created with
>  uppercase letters, you're out of luck.  And if you look for a value
>  for a non-string key, that's no good either:

Yeah, finally I've done:

                class InsensitiveHash < Hash
                        def [](key)
                                find {|h| h[0] =~ /^#{key}$/i }[1]
                        end
                end

It works. :)

--
Iñaki Baz Castillo
<i...@aliax.net>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Klemme  
View profile  
 More options Apr 23 2008, 5:45 am
Newsgroups: comp.lang.ruby
From: Robert Klemme <shortcut...@googlemail.com>
Date: Wed, 23 Apr 2008 04:45:55 -0500
Local: Wed, Apr 23 2008 5:45 am
Subject: Re: Extending Hast class with custom [] []= methods
2008/4/23, Iñaki Baz Castillo <i...@aliax.net>:

.. and is awfully inefficient.

Here's another way, which is more efficient for large hashes

class CiHash2 < DelegateClass(Hash)
  CiString = Struct.new :string do
    def to_s; string.downcase end
    def hash; string.downcase.hash end
    def eql?(s) string.downcase.eql? s.string.downcase end
    alias == eql?
    def inspect; string.inspect; end
  end

  def initialize
    super({})
  end

  def []=(k,v)
    k = CiString.new(k) if String === k
    __getobj__[k] = v
  end

  def [](k)
    k = CiString.new(k) if String === k
    __getobj__[k]
  end

  # add other lookup and mutation methods
end

h = CiHash2.new
h["FOO"]=3
h["Foo"]=4
puts h["foo"]
puts h["fOo"]
p h

Cheers

robert

--
use.inject do |as, often| as.you_can - without end


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Iñaki Baz Castillo  
View profile  
 More options Apr 23 2008, 6:06 am
Newsgroups: comp.lang.ruby
From: Iñaki Baz Castillo <i...@aliax.net>
Date: Wed, 23 Apr 2008 05:06:07 -0500
Local: Wed, Apr 23 2008 6:06 am
Subject: Re: Extending Hast class with custom [] []= methods
2008/4/23, Robert Klemme <shortcut...@googlemail.com>:

> ... and is awfully inefficient.

>  Here's another way, which is more efficient for large hashes

Oh, thanks, I'll spend some time investigating what your solution (or
how) does :)
Thanks.

--
Iñaki Baz Castillo
<i...@aliax.net>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google