Ruby 同好会宣言

Preview:

DESCRIPTION

RubyDoukoukai StatementRuby Introduction for PHPer

Citation preview

RubyRuby同好会宣言同好会宣言

RubyDoukoukai StatementRubyDoukoukai 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

文字列出力

PHPecho "Hello, World!";

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

変数代入

PHP$foo = "bar";

Rubyfoo = "bar"

定数定義

PHPdefine("FOO", 1);

RubyFOO = 1

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

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

条件分岐

PHP

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

Ruby

positive = if n > 0 trueelse falseend

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 + ")" endend

foo = Foo.newputs 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 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"

$this->_name$this->_foundedYear

@name@founded_year

public function getFoundedYear() { ~~~ }

attr_reader :founded_year

public functiongetName() { ~~~ }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 endend

多重継承

class Iroha include Enumerable include Foo include Bar

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

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同好会宣言

ご清聴ありがとう

ございました

Recommended