redefine [] operator in ruby
// the parameter must be an integer/fixnum
//
// i.e.
// def [](chunk)
// @internal_id_hash[chunk.chunk_id]
// end
// is not possible
class ChunkList .. def [](chunk_id) @internal_id_hash[chunk_id] end end
11366 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
class ChunkList .. def [](chunk_id) @internal_id_hash[chunk_id] end end
>>> a = [5, 2, 3, 1, 4] >>> a.sort() # become [1, 2, 3, 4, 5] >>> a.sort(cmp) # same as above >>> a = "This is a test string from Andrew".split() >>> a.sort(key=str.lower) # using key is simpler # ['a', 'Andrew', 'from', 'is', 'string', 'test', 'This'] >>> import operator >>> L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)] >>> sorted(L, key=operator.itemgetter(1)) [('d', 1), ('c', 2), ('b', 3), ('a', 4)] # itemgetter allows easy Schwartzian transform
(++i < i-- && j++ > j--)