hamstergem / hamster

Efficient, Immutable, Thread-Safe Collection classes for Ruby
Other
1.89k stars 94 forks source link

Suggestion: add Hash#[]= which directs users to use #put instead #219

Closed dubek closed 8 years ago

dubek commented 8 years ago

Not sure if it's a good idea but thought I'd raise it anyway:

2.2.3 :001 > h = Hamster::Hash[:a => 5]
 => Hamster::Hash[:a => 5] 
2.2.3 :002 > h[:b] = 6
NoMethodError: undefined method `[]=' for Hamster::Hash[:a => 5]:Hamster::Hash
    from (irb):2
    from /home/dmurik/.rvm/rubies/ruby-2.2.3/bin/irb:11:in `<main>'
2.2.3 :003 > class Hamster::Hash
2.2.3 :004?>   def []=(*)
2.2.3 :005?>     raise NoMethodError, "Hamster::Hash doesn't support `[]='; use `put' instead"
2.2.3 :006?>     end
2.2.3 :007?>   end
 => :[]= 
2.2.3 :008 > h[:b] = 6
NoMethodError: Hamster::Hash doesn't support `[]='; use `put' instead
    from (irb):5:in `[]='
    from (irb):8
    from /home/dmurik/.rvm/rubies/ruby-2.2.3/bin/irb:11:in `<main>'

And same for Vector#[]=, I guess.

(As a bonus we get the string []=(*) in our code which is like an emoticon of a small robot.)

alexdowad commented 8 years ago

I like it!

dubek commented 8 years ago

Of course it'll need to have special YARD directive so it doesn't appear in the API docs.

@xaviershay Your opinion about this suggestion? Any idea if any other library acts in a similar fashion?

Bill commented 8 years ago

This is a good idea. I was surprised a week or so ago, when I found no []= method on a Hamster Hash object. I thought I'd done something wrong until I went and confirmed my suspicion in the source code.