Skip to content

Instantly share code, notes, and snippets.

@kachick
Created September 24, 2012 04:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kachick/3774165 to your computer and use it in GitHub Desktop.
Save kachick/3774165 to your computer and use it in GitHub Desktop.
ArrayやHashに於ける、Index/Key外へのアクセス

Index/Key外へのアクセス

get
----
      |Array  |Hash
-----------------------
厳しい |fetch  |fetch
緩やか |[], at |[]


set
----
      |Array  |Hash
-----------------------
厳しい |?      |?         #=> ここが?
緩やか |[]=    |[]=, store
class Array
  def lesser_map!
    each_with_index do |val, idx|
      self[idx] = yield(val)      #=> この長さなら自明だとは思うものの、こういったケースでIndex外へアクセスするのはまずバグだと思います。
    end
  end
end

class Array
  def lesser_map!
    each_with_index do |val, idx|
      REPLACE idx, yield(val)      #=> Index外アクセスへ例外を出すようなメソッドがあれば、こういった場合に優先して使いたいと思います。
    end
  end
end
@kachick
Copy link
Author

kachick commented Sep 29, 2012

@kachick
Copy link
Author

kachick commented Jan 5, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment