Ruby
同好会宣言
RubyDoukoukai Statement

   2010/09/14 (Tue)
    @yuya_takeyama
Rubyと私
・日曜Rubyist
・趣味で1年ほど
・仕事ではコード生成などに
・アプリ構築経験ほぼ無し
・たまにブログでRubyの話
                  http://blog.yuyat.jp
・GitHubにRubyコード
     http://github.com/yuya-takeyama
Agenda

第一部
Rubyの特徴
第二部
Ruby同好会宣言
Rubyの特徴 #1



シンプルな構文
文字列出力
PHP
echo "Hello, World!";

Ruby
print "Hello, World!"
puts "Hello, World!"
変数代入
PHP
$foo = "bar";

Ruby
foo = "bar"
定数定義
PHP
define("FOO", 1);

Ruby
FOO = 1
     「変数は小文字で始める」
  「定数(クラス名)は大文字で始める」
というルールが言語仕様として決められている
条件分岐
PHP
if ($n > 0) {
    $positive = true;
} else {
    $positive = false;
}
Ruby
positive = if n > 0
  true
else
  false
end
      Rubyにおけるifは式なので、
    それ自体で返り値を持つことができる。
クラス定義
メソッド呼び出し
PHP

class Foo
{
    public function bar($str)
    {
        return "(" . $str . ")";
    }
}

$foo = new Foo;
echo $foo->bar("baz");
Ruby

class Foo
  def bar(str)
    "(" + str + ")"
  end
end

foo = Foo.new
puts foo.bar("baz")
Rubyの特徴 #2

   純粋
オブジェクト指向
   言語
何でも
オブジェクト
文字列も
数値も
配列も
Object
例
"foo,bar,baz"
     ↓
"Foo/Bar/Baz"
in PHP ...
join("/", array_map(
    function ($s) { return ucfirst($s); },
    explode(",","foo,bar,baz")
));

=> "Foo/Bar/Baz"
                   in PHP5.3.x

          stringもarrayも
     オブジェクトじゃない (プリミティブ値)
         → メソッドを持たない
join("/", array_map(
    create_function(
        '$s',
        'return ucfirst($s);'
    ),
    explode(",","foo,bar,baz")
));

=> "Foo/Bar/Baz"
                   in PHP5 (< 5.3)
???
in Ruby ...
"foo,bar,baz".split(",").map{|s|s.capitalize}.join("/")

=> "Foo/Bar/Baz"


     StringもArrayもオブジェクト
       → メソッドをつなげられる
メ
ソ
ッドチェイン
流れるようなインターフ
          ェ
          イ
          ス
Ruby

"foo,bar,baz".split(",").map{|s|s.capitalize}.join("/")

=> "Foo/Bar/Baz"


JavaScript

"foo,bar,baz".split(",").map(function (s) {
  return s.capitalize();
}).join("/");

=> "Foo/Bar/Baz"    (ただし、capitalize()は架空メソッド)
Rubyの特徴 #3

オブジェクト指向
  を支える
強力な言語機構
セッター
あるいは
ゲッター
in PHP ...
class Company
{
    protected $_name, $_foundedYear;

    public function __construct($name, $foundedYear)
    {
        $this->_name       = $name;
        $this->_foundedYear = $foundedYear;
    }

    public function setName($name)
    {
        $this->_name = $name;
    }

    public function getName( )
    {
        return $this->_name;
    }

    public function getFoundedYear( )
    {
        return $this->_foundedYear;
    }
}

$company = new Company('YY Company', 2000);
echo $company->getName( ) . ' is founded in ' . $company->getFoundedYear( );
// => "YY Company is founded in 2000"
$company->setName('GayaGaya Company');
echo $company->getName( ) . ' is originally founded in ' . $company->getFoundedYear( );
// => "GayaGaya Company is originally founded in 2000"
in Ruby ...
class Company
  attr_accessor :name
  attr_reader :founded_year

  def initialize(name, founded_year)
    @name         = name
    @founded_year = founded_year
  end
end

company = Company.new('YY Company', 2000)
puts company.name + " is founded in " + comapny.founded_year.to_s
# => "YY Company is founded in 2000"
company.name = "GayaGaya Company"
puts company.name + " is originally founded in " + comapny.founded_year.to_s
# => "GayaGaya Company is originally founded in 2000"
$this->_name
$this->_foundedYear

        ↓

      @name
  @founded_year
public function
getFoundedYear() { ~~~ }

           ↓

attr_reader :founded_year
public function
getName() { ~~~ }
 public function
setName() { ~~~ }

       ↓

attr_accesor :name
継承
in PHP ...
単一
継承
class Dog extends Animal
{
    public function osuwari( )
    {
        $this->shitDown( );
    }
}
多重
継承?
class List implements Iterator,
ArrayAccess, Countable
{
    public function rewind()
    {}

    /* ~~~ */

}
仕様
のみ
継承
実装は
 継承
されない
in Ruby ...
単一
継承
class Dog < Animal
  def osuwari
    shit_down
  end
end
多重
継承
class Iroha
  include Enumerable
  include Foo
  include Bar

  def each
    "いろはにほへと".each_char do |c|
      yield c
    end
  end
end
include Enumerable

  するだけで ...
all?, any?, chunk, collect, collect_concat,
   count, cycle, detect, drop, drop_while,
      each_cons, each_entry, each_slice,
 each_with_index, each_with_object, entries,
find, find_all, find_index, first, flat_map,
 grep, group_by, include?, inject, map, max,
     max_by, member?, min, min_by, minmax,
  minmax_by, none?, one?, partition, reduce,
 reject, reverse_each, select, slice_before,
  sort, sort_by, take, take_while, to_a, zip

これだけのメソッドが使用可能に
ミックスイン
  継承
まとめ

1. Rubyの文法はシンプル
2. Rubyでは何でもオブジェクト
3. Rubyではセッターやゲッターのメソッドを書
かなくていい
4. Rubyではミックスインで多重継承できる
で
Ruby同好会で
    何を
やりたいですか?
【急募】
   あなたの
   わたしの
Ruby同好会宣言
ご清聴
 ありがとう
ございました

Ruby 同好会宣言