Created
February 9, 2013 10:38
-
-
Save higaki/4744812 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.each{|i| puts i} | |
| # >> 0 | |
| # >> 1 | |
| # >> 2 | |
| # >> 3 | |
| # >> 4 | |
| # >> 5 | |
| # >> 6 | |
| # >> 7 | |
| # >> 8 | |
| # >> 9 | |
| # 各要素を 2倍した値を持つ配列を作ろう | |
| a.map{|i| i * 2} # => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] | |
| # 全要素の合計値を計算しよう | |
| a.inject{|s, i| s + i} # => 45 | |
| ## マニアが書くと | |
| a.inject(:+) # => 45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment