[ruby-list:50595] Re: [質問] yield の使い方

From: Nobuyoshi Nakada <nobu@...>
Date: 2017-10-05 13:14:50 UTC
List: ruby-list #50595
なかだです。

On 2017/10/05 19:56, yamataka@u08.itscom.net wrote:
> dlna_play_list = [
>   {:ch  => '2',
>    :bit => '16',
>    :fs  => '32000'},

値は文字列ではなく数値で持っておいたほうがいいのではないでしょうか。

>   def mk_play_list(type)
>     case type
>     when :fs,:bit,:ch
>       @play_list = @play_list.select { |content| yield(content[type].to_i) }
>     end
>   end

  def mk_play_list(*types)
    types &= [:fs,:bit,:ch]
    @play_list.select { |content| yield(*content.values_at(*types))}
  end

> fs,bit,ch の条件を自由に組合わせたブロックを渡して、
>
> { |fs,bit,ch|
>   (fs > 32000 && fs < 96000) && (bit >= 16)
>   # 要素条件を全て指定しなくてもよいようにしたい
> }

pp obj.mk_play_list(:fs, :bit, :ch) { |fs,bit,ch|
  (fs > 32000 && fs < 96000) && (bit >= 16)
}

でどうでしょうか。

-- 
--- 僕の前にBugはない。
--- 僕の後ろにBugはできる。
    中田 伸悦

In This Thread