ruby - How to distribute a conditional through an n-dimensional array -
[a, b].fetch(x) notionally equivalent following method:
def fetch(x) if x == 0 elsif x == 1 b end end this generalises, e.g. [[a, b], [c, d]].fetch(x) equivalent to:
def fetch(x) if x == 0 [a, b] elsif x == 1 [c, d] end end which equivalent to:
def fetch(x) [ if x == 0 elsif x == 1 c end, if x == 0 b elsif x == 1 d end ] end in case, conditional has been distributed each of elements within array.
if have n-dimensional array, how can dynamically define #fetch(x) array:
def define_fetch(some_array) define_method :fetch |x| # ??? end end define_fetch([[[a, b], [c, d]], [[e, f], [g, h]]]) i'm interested in latter example conditionals distributed within each of resulting array's elements.
Comments
Post a Comment