diary

I like Hatena Star with a text selection.

2020-04-21

最新のmacOS(Catalina)にデフォルトで入っているRubyは2.6.3で、1つ前のmojaveは2.3.7ということを知った。 つまり保守的に1つ前のmacOSをサポートするには2.3で動くコードを書く必要があるのだなあ。


twitter.com

りんごジュース美味しい情報をゲットした。 定期的に買いたいのだけど、Amazonって定期お得便設定がある商品以外は定期的に買えないのかな。


class KeywordEnumerator
  def initialize(e)
    @enumerator = e
  end

  def method_missing(name, *args, **kwargs, &block)
    if @enumerator.respond_to?(name)
      @enumerator.__send__(name, *args, **kwargs) do |hash|
        block.call(**hash)
      end
    else
      super
    end
  end

  def respond_to_missing?(symbol, include_private)
    @enumerator.respond_to?(symbol, include_private) || super
  end
end

module Enumerable
  def to_kw
    KeywordEnumerator.new(self)
  end
end

pp [{ a: 1, b: 2 }, { a: 10, b: 20 }]
  .to_kw
  .map { |a:, b:| [a, b] }
# => [[1, 2], [10, 20]]