F
Florian Gross
Moin.
I just did a quick implementation of Hash::MixIn (you provide .keys,
..fetch, .store and .delete and get a full-blown Hash interface) and
Python style Hash access to an Object's methods.
Here's a quick sample IRB session:
irb(main):001:0> obj = "foo"
=> "foo"
irb(main):002:0> obj.dict[:reverse] = "Let's lie.".dict[:reverse]
=> #<Method: String#reverse>
irb(main):003:0> obj.reverse
=> ".eil s'teL"
irb(main):004:0> obj.dict.values_atreverse, :+)
=> [#<Method: "foo".reverse>, #<Method: String#+>]
irb(main):005:0> obj.dict.reject! { |key, value| key.to_s[0] == ?r }
=> #<MethodDict: :send=>#<Method: String(Kernel)#send>, ...>
irb(main):006:0> obj.dict.include?rindex)
=> false
The implementation of all this is available at
http://flgr.0x42.net/method-dict.rb
I might include Hash::MixIn as a sample in the ruby-contract library as
it would be a good case for automatically adding to the interface when a
given Contract is met.
Anyway, does any part of this look useful to you?
I've heard quite a few requests for a Hash mix-in, but I'm not sure how
to best make this available to the general public.
Regards,
Florian Gross
I just did a quick implementation of Hash::MixIn (you provide .keys,
..fetch, .store and .delete and get a full-blown Hash interface) and
Python style Hash access to an Object's methods.
Here's a quick sample IRB session:
irb(main):001:0> obj = "foo"
=> "foo"
irb(main):002:0> obj.dict[:reverse] = "Let's lie.".dict[:reverse]
=> #<Method: String#reverse>
irb(main):003:0> obj.reverse
=> ".eil s'teL"
irb(main):004:0> obj.dict.values_atreverse, :+)
=> [#<Method: "foo".reverse>, #<Method: String#+>]
irb(main):005:0> obj.dict.reject! { |key, value| key.to_s[0] == ?r }
=> #<MethodDict: :send=>#<Method: String(Kernel)#send>, ...>
irb(main):006:0> obj.dict.include?rindex)
=> false
The implementation of all this is available at
http://flgr.0x42.net/method-dict.rb
I might include Hash::MixIn as a sample in the ruby-contract library as
it would be a good case for automatically adding to the interface when a
given Contract is met.
Anyway, does any part of this look useful to you?
I've heard quite a few requests for a Hash mix-in, but I'm not sure how
to best make this available to the general public.
Regards,
Florian Gross