Ruby

Similar documents
ruby-intro.key

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

数据结构与算法 - Python基础

02

untitled

Microsoft Word - 01.DOC

Microsoft Word - ch04三校.doc

untitled

WWW PHP

新・解きながら学ぶJava

Microsoft PowerPoint - plan08.ppt

Microsoft Word - 第3章.doc

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.07.doc

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

C/C++ - 字符输入输出和字符确认

Java

Fuzzy Highlight.ppt

FY.DOC

C/C++程序设计 - 字符串与格式化输入/输出

第5章修改稿

Chapter 1 What is Programing Paradigm 1

WWW PHP Comments Literals Identifiers Keywords Variables Constants Data Types Operators & Expressions 2

EJB-Programming-4-cn.doc

Perl

EJB-Programming-3.PDF

前言 C# C# C# C C# C# C# C# C# microservices C# More Effective C# More Effective C# C# C# C# Effective C# 50 C# C# 7 Effective vii

CHAPTER 1

全国计算机技术与软件专业技术资格(水平)考试

untitled

VHDL(Statements) (Sequential Statement) (Concurrent Statement) VHDL (Architecture)VHDL (PROCESS)(Sub-program) 2

C/C++ 语言 - 循环

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

06 01 action JavaScript action jquery jquery AJAX CSS jquery CSS jquery HTML CSS jquery.css() getter setter.css('backgroundcolor') jquery CSS b

Microsoft Word - 物件導向編程精要.doc

无类继承.key

untitled

CC213

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

(Microsoft Word - 11\244T\246\342\277\337\260l\302\334.doc)

CHAPTER VC#

2 WF 1 T I P WF WF WF WF WF WF WF WF 2.1 WF WF WF WF WF WF

C/C++语言 - 运算符、表达式和语句

untitled

1 Framework.NET Framework Microsoft Windows.NET Framework.NET Framework NOTE.NET NET Framework.NET Framework 2.0 ( 3 ).NET Framework 2.0.NET F

Microsoft PowerPoint - OPVB1基本VB.ppt

Chapter 9: Objects and Classes

Microsoft Word - PHP7Ch01.docx

Microsoft PowerPoint - ch6 [相容模式]

untitled

個 人 的 手, 拉 著 瞎 子 的 手 把 他 帶 往 村 外 的 時 候, 對 於 瞎 子 來 講, 那 個 人 的 手 和 耶 穌 的 手 有 沒 有 區 別? 沒 有! 為 什 麼 沒 有 區 別? 因 為 對 於 一 個 瞎 子 來 說, 手 和 耳 朵 就 是 他 接 觸 世 界, 瞭

C/C++ - 字符串与字符串函数

Progperl.PDF

基于CDIO一体化理念的课程教学大纲设计

51 C 51 isp 10 C PCB C C C C KEIL

新版 明解C++入門編

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

untitled

Improved Preimage Attacks on AES-like Hash Functions: Applications to Whirlpool and Grøstl

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

mvc

untitled

RunPC2_.doc

LEETCODE leetcode.com 一 个 在 线 编 程 网 站, 收 集 了 IT 公 司 的 面 试 题, 包 括 算 法, 数 据 库 和 shell 算 法 题 支 持 多 种 语 言, 包 括 C, C++, Java, Python 等 2015 年 3 月 份 加 入 了 R

Microsoft Word - Learn Objective-C.doc

Microsoft Word - linux命令及建议.doc

epub83-1

<4D F736F F D D342DA57CA7DEA447B14D2DA475B57BBB50BADEB27AC3FEB14DA447B8D5C344>

我是誰 張 文鈿 a.k.a. ihower 來 自台灣

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

CC213

ebook14-4

Learning Java

ebook50-15

PowerPoint プレゼンテーション

untitled

< FB14EA5D028B2C4A447A6B8B4ABC0C9295FB05DAC46B3A1A4BDB0C8ADFBB9EFA644A8C6B0BBAC64C576AF715FA4BAADB D6D5FB3E6A6E22E696E6464>

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

, 即 使 是 在 昏 暗 的 灯 光 下, 她 仍 然 可 以 那 么 耀 眼 我 没 有 地 方 去, 你 会 带 着 我 么 杜 晗 像 是 在 嘲 笑 一 般, 嘴 角 的 一 抹 冷 笑 有 着 不 适 合 这 个 年 龄 的 冷 酷 和 无 情, 看 着 江 华 的 眼 神 毫 无 温

untitled

OOP with Java 通知 Project 4: 4 月 18 日晚 9 点 关于抄袭 没有分数

四川省普通高等学校

<4D F736F F D20B0D7D0A1C1AEBDE0D7A8BFAF C5C5B0E E646F63>

27 :OPC 45 [4] (Automation Interface Standard), (Costom Interface Standard), OPC 2,,, VB Delphi OPC, OPC C++, OPC OPC OPC, [1] 1 OPC 1.1 OPC OPC(OLE f

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

2/80 2

(procedure-oriented)?? 2

chp6.ppt

穨文件1

基于矩阵分解和矩阵变换的多义词向量研究

《大话设计模式》第一章

概述

Chapter 9: Objects and Classes

Strings

Microsoft PowerPoint - RT0950_EliminatingRubyGILthroughHTM_Slides_ja.ppt

C

javaexample-02.pdf

多層次傳銷與獎金系統

read.pdf

0SQL SQL SQL SQL SQL 3 SQL DBMS Oracle DBMS DBMS DBMS DBMS RDBMS R DBMS 2 DBMS RDBMS R SQL SQL SQL SQL SELECT au_fname,au_ lname FROM authors ORDER BY

Transcription:

Ruby ihower@gmail.com http://creativecommons.org/licenses/by-nc/2.5/tw/

? a.k.a. ihower http://ihower.idv.tw/blog/ twitter: ihower 2006 Ruby ( ) Rails Developer http://handlino.com http://registrano.com

Ruby? (interpreted) Yukihiro Matsumoto, a.k.a. Matz Lisp, Perl, Smalltalk Happy

irb: Interactive Ruby irb(main):001:0> irb(main):001:0> 1 + 1 => 2 irb(main):002:0>

Ruby Dynamic v.s. Static typing Ruby/Perl/Python/PHP v.s. Java/C/C++ Strong v.s. Weak typing Ruby/Perl/Python/Java v.s. PHP/C/C++

?? PHP code: Ruby code: $i = 1; echo "Value is " + $i # 1 C code: i=1 puts "Value is " + i #TypeError: can't convert Fixnum into String # from (irb):2:in `+' # from (irb):2 int a = 5; float b = a;

Ruby # "UPPER" puts "upper".upcase # -5 puts -5.abs # Fixnum puts 99.class # "Ruby Rocks!" 5.times do puts "Ruby Rocks!"

Array a = [ 1, "cat", 3.14 ] puts a[0] # 1 puts a.size # 3 a[2] = nil puts a.inspect # [1, "cat", nil]

Hash (Associative Array) config = { "foo" => 123, "bar" => 456 } puts config["foo"] # 123

Symbols config = { :foo => 123, :bar => 456 } puts config[:foo] # 123

If if account.total > 100000 puts "large account" elsif account.total > 25000 puts "medium account" else puts "small account"

If Perl Style if account.total > 100000 puts "large account" elsif account.total > 25000 puts "medium account" else puts "small account"

expression? true_expresion : false_expression x = 3 puts ( x > 3 )? " " : " " #

Case case name when "John" puts "Howdy John!" when "Ryan" puts "Whatz up Ryan!" else puts "Hi #{name}!"

while, loop, until, next and break i=0 while ( i < 10 ) i += 1 next if i % 2 == 0 # i = 0 loop do i += 1 break if i > 10 # i = 0 i += 1 until i > 10 puts i # 11

false nil puts "not execute" if nil puts "not execute" if false puts "execute" if true # execute puts "execute" if # execute ( JavaScript ) puts "execute" if 0 # execute ( C ) puts "execute" if 1 # execute puts "execute" if "foo" # execute puts "execute" if Array.new # execute

Regular Expressions Perl # phone = "123-456-7890" if phone =~ /(\d{3})-(\d{3})-(\d{4})/ ext = $1 city = $2 num = $3

Methods def def say_hello(name) result = "Hi, " + name return result puts say_hello('ihower') # Hi, ihower

Methods def def say_hello(name) result = "Hi, " + name return result puts say_hello('ihower') # Hi, ihower

Class class Greeter def initialize(name) @name = name def say(word) puts "#{word}, #{@name}" g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("hello") # Hello, ihower g2.say("hello") # Hello, ihover

Class class Greeter def initialize(name) @name = name def say(word) puts "#{word}, #{@name}" g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("hello") # Hello, ihower g2.say("hello") # Hello, ihover

Class class Greeter def initialize(name) @name = name def say(word) puts "#{word}, #{@name}" g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("hello") # Hello, ihower g2.say("hello") # Hello, ihover

Class class Greeter def initialize(name) @name = name def say(word) puts "#{word}, #{@name}" g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("hello") # Hello, ihower g2.say("hello") # Hello, ihover

Class class Greeter def initialize(name) @name = name def say(word) puts "#{word}, #{@name}" g1 = Greeter.new("ihower") g2 = Greeter.new("ihover") g1.say("hello") # Hello, ihower g2.say("hello") # Hello, ihover

Class ( ) class Greeter @@name = ihower def self.say puts @@name Greeter.say # Hello, ihower

Class ( ) class Greeter @@name = ihower def self.say puts @@name Greeter.say # Hello, ihower

Class ( ) class Greeter @@name = ihower def self.say puts @@name Greeter.say # Hello, ihower

Class body attr_accessor, attr_writer, attr_reader class Person class Person attr_accessor :name def name @name def name=(val) @name = val

class MyClass def public_method private def private_method protected def protected_method class MyClass def public_method def private_method def protected_method public :public_method private :private_method protected :proected_method

Class class Pet attr_accessor :name, :age class Cat < Pet class Dog < Pet

code block closure { puts "Hello" } # block do puts "Blah" # block puts "Blah"

code block (iterator) # people = ["David", "John", "Mary"] people.each do person puts person # 5.times { puts "Ruby rocks!" } # 1.upto(9) { x puts x }

code block (iterator) # people = ["David", "John", "Mary"] people.each do person puts person # 5.times { puts "Ruby rocks!" } # 1.upto(9) { x puts x } while, until, for

code block # a = [ "a", "b", "c", "d" ] b = a.map { x x + "!" } puts b.inspect # ["a!", "b!", "c!", "d!"] # b = [1,2,3].find_all{ x x % 2 == 0 } b.inspect # [2]

code block # a = [ "a", "b", "c" ] a.delete_if { x x >= "b" } # ["a"] # [2,1,3].sort! { a, b b <=> a } # ["3", 2, 1 ]

code block functional programming fu? # (5..10).inject { sum, n sum + n } # find the longest word longest = ["cat", "sheep", "bear"].inject do memo, word ( memo.length > word.length )? memo : word

code block file = File.new("testfile", "r") #... file.close File.open("testfile", "r") do file #... #

Yield yield code block # def call_block puts "Start" yield yield puts "End" call_block { puts "Blocks are cool!" } # # "Start" # "Blocks are cool!" # "Blocks are cool!" # "End"

code block def call_block yield(1) yield(2) yield(3) call_block { i puts "#{i}: Blocks are cool!" } # # "1: Blocks are cool!" # "2: Blocks are cool!" # "3: Blocks are cool!"

Proc object code block def call_block(&block) block.call(1) block.call(2) block.call(3) call_block { i puts "#{i}: Blocks are cool!" } # proc object proc_1 = Proc.new { i puts "#{i}: Blocks are cool!" } proc_2 = lambda { i puts "#{i}: Blocks are cool!" } call_block(&proc_1) call_block(&proc_2) # # "1: Blocks are cool!" # "2: Blocks are cool!" # "3: Blocks are cool!"

def my_sum(*val) val.inject(0) { sum, v sum + v } puts my_sum(1,2,3,4) # 10

Hash { } def my_print(a, b, options) puts a puts b puts options[:x] puts options[:y] puts options[:z] puts my_print("a", "B", { :x => 123, :z => 456 } ) puts my_print("a", "B", :x => 123, :z => 456) # # A # B # 123 # nil # 456

raise, begin, rescue, ensure raise "Not works!!" # RuntimeError # class MyException < RuntimeError raise MyException begin puts 10 / 0 rescue => e puts e.class ensure #... # ZeroDivisionError

Module (1) Namespace module MyUtil def self.foobar puts "foobar" MyUtil.foobar # foobar

Module(2) Mixins module Debug def who_am_i? "#{self.class.name} (\##{self.object_id}): #{self.to_s}" class Foo include Debug # Mixin #... class Bar include Debug include AwesomeModule #... ph = Foo.new("12312312") et = Bar.new("78678678") ph.who_am_i? # "Foo (#330450): 12312312" et.who_am_i? # "Bar (#330420): 78678678"

Module(2) Mixins module Debug def who_am_i? "#{self.class.name} (\##{self.object_id}): #{self.to_s}" class Foo include Debug # Mixin #... class Bar include Debug include AwesomeModule #... ph = Foo.new("12312312") et = Bar.new("78678678") Ruby Module ph.who_am_i? # "Foo (#330450): 12312312" et.who_am_i? # "Bar (#330420): 78678678"

(duck typing) # class Duck def quack puts "quack!" # ( ) class Mallard def quack puts "qwuaacck!! quak!"

Class Type birds = [Duck.new, Mallard.new, Object.new] # birds.each do duck duck.quack if duck.respond_to? :quack

Class Type birds = [Duck.new, Mallard.new, Object.new] # birds.each do duck duck.quack if duck.respond_to? :quack OOP!

Metaprogramming

define_method class Dragon define_method(:foo) { puts "bar" } ['a','b','c','d','e','f'].each do x define_method(x) { puts x } dragon = Dragon.new dragon.foo # "bar" dragon.a # "a" dragon.f # "f"

Domain-Specific Language class Firm < ActiveRecord::Base has_many :clients has_one :account belongs_to :conglomorate # has_many AciveRecord class method Firm instance methods firm = Firm.find(1) firm.clients firm.clients.size firm.clients.build firm.clients.destroy_all

Method Missing class Proxy def initialize(object) @object = object def method_missing(symbol, *args) @object.s(symbol, *args) object = ["a", "b", "c"] proxy = Proxy.new(object) puts proxy.first # Proxy first method_missing "a"

Introspection ( ) # Object.methods => ["s", "name", "class_eval", "object_id", "new", "singleton_methods",...] # Object.respond_to? :name => true

Ruby

Ruby production Ruby 1.8.6, 1.8.7 ( MRI, Matz Ruby Interpreter) Ruby 1.9.1 (YARV) JRuby Ruby1.8.6 1.9 Ruby Enterprise Edition(REE) Ruby 1.8.6 GC memory leak

Ruby production IronRuby (based on Microsoft.NET) MacRuby (based on Objective-C) Rubinius (Engine yard project) MagLev (based on smalltalk) Cardinal (based on Parrot VM)

Thank you. Beginning Ruby 2nd. (Apress) Programming Ruby (The Pragmatic Programmers) The Well-Grounded Rubyist (Manning) Ruby (O Reilly)