ruby 同好会宣言

63
Ruby Ruby 同好会宣言 同好会宣言 RubyDoukoukai Statement RubyDoukoukai Statement 2010/09/14 (Tue) @yuya_takeyama

Upload: yuya-takeyama

Post on 13-Jan-2015

1.236 views

Category:

Technology


9 download

DESCRIPTION

RubyDoukoukai StatementRuby Introduction for PHPer

TRANSCRIPT

Page 1: Ruby 同好会宣言

RubyRuby同好会宣言同好会宣言

RubyDoukoukai StatementRubyDoukoukai Statement

2010/09/14 (Tue)@yuya_takeyama

Page 2: Ruby 同好会宣言

Rubyと私

Page 3: Ruby 同好会宣言

・日曜Rubyist・趣味で1年ほど・仕事ではコード生成などに・アプリ構築経験ほぼ無し・たまにブログでRubyの話

http://blog.yuyat.jp

・GitHubにRubyコードhttp://github.com/yuya-takeyama

Page 4: Ruby 同好会宣言

Agenda

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

Page 5: Ruby 同好会宣言

シンプルな構文

Rubyの特徴 #1

Page 6: Ruby 同好会宣言

文字列出力

Page 7: Ruby 同好会宣言

PHPecho "Hello, World!";

Rubyprint "Hello, World!"puts "Hello, World!"

Page 8: Ruby 同好会宣言

変数代入

Page 9: Ruby 同好会宣言

PHP$foo = "bar";

Rubyfoo = "bar"

Page 10: Ruby 同好会宣言

定数定義

Page 11: Ruby 同好会宣言

PHPdefine("FOO", 1);

RubyFOO = 1

「変数は小文字で始める」「定数(クラス名)は大文字で始める」

というルールが言語仕様として決められている

Page 12: Ruby 同好会宣言

条件分岐

Page 13: Ruby 同好会宣言

PHP

if ($n > 0) { $positive = true;} else { $positive = false;}

Page 14: Ruby 同好会宣言

Ruby

positive = if n > 0 trueelse falseend

Rubyにおけるifは式なので、それ自体で返り値を持つことができる。

Page 15: Ruby 同好会宣言

クラス定義メソッド呼び出し

Page 16: Ruby 同好会宣言

PHP

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

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

Page 17: Ruby 同好会宣言

Ruby

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

foo = Foo.newputs foo.bar("baz")

Page 18: Ruby 同好会宣言

純粋オブジェクト指向

言語

Rubyの特徴 #2

Page 19: Ruby 同好会宣言

何でもオブジェクト

Page 20: Ruby 同好会宣言

文字列も

Page 21: Ruby 同好会宣言

数値も

Page 22: Ruby 同好会宣言

配列も

Page 23: Ruby 同好会宣言

Object

Page 24: Ruby 同好会宣言

"foo,bar,baz"↓

"Foo/Bar/Baz"

Page 25: Ruby 同好会宣言

in PHP ...

Page 26: Ruby 同好会宣言

join("/", array_map( function ($s) { return ucfirst($s); }, explode(",","foo,bar,baz")));

=> "Foo/Bar/Baz"

in PHP5.3.x

stringもarrayもオブジェクトじゃない (プリミティブ値)

→ メソッドを持たない

Page 27: Ruby 同好会宣言

join("/", array_map( create_function( '$s', 'return ucfirst($s);' ), explode(",","foo,bar,baz")));

=> "Foo/Bar/Baz"in PHP5 (< 5.3)

Page 28: Ruby 同好会宣言

???

Page 29: Ruby 同好会宣言

in Ruby ...

Page 30: Ruby 同好会宣言

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

=> "Foo/Bar/Baz"

StringもArrayもオブジェクト→ メソッドをつなげられる

Page 31: Ruby 同好会宣言

メソッドチェイン

Page 32: Ruby 同好会宣言

流れるようなインターフェイス

Page 33: Ruby 同好会宣言

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()は架空メソッド)

Page 34: Ruby 同好会宣言

オブジェクト指向を支える

強力な言語機構

Rubyの特徴 #3

Page 35: Ruby 同好会宣言

セッターあるいはゲッター

Page 36: Ruby 同好会宣言

in PHP ...

Page 37: Ruby 同好会宣言

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"

Page 38: Ruby 同好会宣言

in Ruby ...

Page 39: Ruby 同好会宣言

class Company attr_accessor :name attr_reader :founded_year

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

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"

Page 40: Ruby 同好会宣言

$this->_name$this->_foundedYear

@name@founded_year

Page 41: Ruby 同好会宣言

public function getFoundedYear() { ~~~ }

attr_reader :founded_year

Page 42: Ruby 同好会宣言

public functiongetName() { ~~~ }public function

setName() { ~~~ }

attr_accesor :name

Page 43: Ruby 同好会宣言

継承

Page 44: Ruby 同好会宣言

in PHP ...

Page 45: Ruby 同好会宣言

単一継承

Page 46: Ruby 同好会宣言

class Dog extends Animal{ public function osuwari() { $this->shitDown(); }}

Page 47: Ruby 同好会宣言

多重継承?

Page 48: Ruby 同好会宣言

class List implements Iterator, ArrayAccess, Countable{ public function rewind() {}

/* ~~~ */

}

Page 49: Ruby 同好会宣言

仕様のみ継承

Page 50: Ruby 同好会宣言

実装は継承

されない

Page 51: Ruby 同好会宣言

in Ruby ...

Page 52: Ruby 同好会宣言

単一継承

Page 53: Ruby 同好会宣言

class Dog < Animal def osuwari shit_down endend

Page 54: Ruby 同好会宣言

多重継承

Page 55: Ruby 同好会宣言

class Iroha include Enumerable include Foo include Bar

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

Page 56: Ruby 同好会宣言

include Enumerable

するだけで ...

Page 57: Ruby 同好会宣言

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

これだけのメソッドが使用可能に

Page 58: Ruby 同好会宣言

ミックスイン継承

Page 59: Ruby 同好会宣言

まとめ

1. Rubyの文法はシンプル2. Rubyでは何でもオブジェクト3. Rubyではセッターやゲッターのメソッドを書かなくていい4. Rubyではミックスインで多重継承できる

Page 60: Ruby 同好会宣言

Page 61: Ruby 同好会宣言

Ruby同好会で何を

やりたいですか?

Page 62: Ruby 同好会宣言

【急募】あなたのわたしの

Ruby同好会宣言

Page 63: Ruby 同好会宣言

ご清聴ありがとう

ございました