i'd use handy util methods; in particular enumerable#to_a on objects satisfy enumerable contract (i.e. implement each), not include enumerable.
i've figured out seems work:
enumerable.instance_method(:to_a).bind(obj).call but seems wordy , un-rubyesque. there better/cleaner/faster way of doing this? i'd prefer not modifying underlying object avoid unintentional consequences.
essentially, i'm looking javascript's apply or call.
edit: motivation encapsulation: i'd objects in classes didn't write, don't control, don't need understand in depth, , may react badly having own methods possibly replaced generic implementation. realize including enumerable work in cases, when goes wrong it's liable pain debug, if there's simple way mixin benefits without being invasive, i'd prefer that.
it sounds whatever reason don't want modify class definition include enumerable can of course extend specific instances.
so have indicated obj implements each can this:
obj.extend(enumerable).to_a ah, original question thought goal not modify class, extending instance logical solution. however, sounds don't want modify class or instance.
i following then:
obj.to_enum.to_a
Comments
Post a Comment