Created
February 9, 2013 10:39
-
-
Save higaki/4744816 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/env ruby | |
| # -*- coding: utf-8; -*- | |
| RUBY_DESCRIPTION # => "ruby 1.9.3p385 (2013-02-06 revision 39114) [x86_64-darwin10.8.0]" | |
| # 0 から 9 までの数値をもつ配列 a がある。 | |
| a = (0..9).to_a # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | |
| # 奇数の要素だけを持つ配列を作ろう | |
| a.select{|i| i.odd?} # => [1, 3, 5, 7, 9] | |
| # ただし odd? メソッドは使用禁止 | |
| # 2 で割った余りが 1 なら奇数 | |
| a.select{|i| i % 2 == 1} # => [1, 3, 5, 7, 9] | |
| # 偶数でなければ、それは奇数 | |
| a.select{|i| !i.even?} # => [1, 3, 5, 7, 9] | |
| # rubyist は true/false の否定を嫌う | |
| # reject は式が真の要素を捨て去る | |
| a.reject{|i| i.even?} # => [1, 3, 5, 7, 9] | |
| ## マニアが書くと | |
| a.reject(&:even?) # => [1, 3, 5, 7, 9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment