The Ruby Language [@+]pragdave dave@pragprog.com
Ruby • A tool for communication • A tool for exploration • A notation (like mathematics)
3
Ken Iverson • Creator of APL (1962) • 1979 Turing Award Winner 4
APL • Sum first 5 numbers: +/ ⍳ 5 • Prime numbers from 1 to R: (~R ∊ R ∘ . × R)/R ← 1 � ⍳ R • Game of Life: life ← { ↑ 1 ⍵ � . ∧ 3 4=+/, ¯ 1 0 1 ∘ . ⊖ ¯ 1 0 1 ∘ . ⌽ ⊂ ⍵ } 5
Ken Iverson • Creator of APL (1962) • 1979 Turing Award Winner 6
Good Notation • Ease of expressing constructs arising in problems. • Suggestivity. • Ability to subordinate detail. • Economy. • Amenability to formal proofs. 7
Good Notation • Code close to the problem. • Help you discover new insights. • Build layers of abstraction. • Small set of basic concepts. • Amenability to formal proofs. 8
Ruby • Code close to the problem. • Help you discover new insights. • Build layers of abstraction. • Small set of basic concepts. • Easy to prove code correct. 9
Ruby • Code close to the problem. • Help you discover new insights. • Build layers of abstraction. • Small set of basic concepts. • Easy to make code correct. 10
Ruby and Language 11
The limits of my language are the limits of my world Ludwig Wittgenstein— Logico-Tractatus Philosophicus 12
The limits of my language are the limits of my world Words? Idioms 13
Idiom • Words that combine to have special meaning • Meaning shared between speaker and listener 14
Idiom “Yoshi kicked the bucket” 15
“Yoshi kicked the bucket” 16
横 飯 17
横飯 18
Idiom == == Pattern • Small set of words with very deep meaning • Very e ffi cient way of communicating • Di ff erence between stranger and native user 19
Idiom == Pattern • Idioms of C++ development • Language-speci fi c 20
Idioms in Ruby int arr[6] = { 4, 12, 9, 13, 3, 1 }; const int size = sizeof(arr)/sizeof(arr[0]); int sum = 0; for (int i = 0; i < size; i++) { sum = sum + arr[i]; } printf("%d\n", sum); 21
Idioms in Ruby arr = [ 4, 12, 9, 13, 3, 1 ] sum = 0 for i in 0..arr.length-1 sum = sum + arr[i] end puts sum 22
Idioms in Ruby arr = [ 4, 12, 9, 13, 3, 1 ] sum = 0 for i in 0...arr.length sum = sum + arr[i] end puts sum 23
Idioms in Ruby arr = [ 4, 12, 9, 13, 3, 1 ] sum = 0 for i in 0...arr.length sum += arr[i] end puts sum 24
Idioms in Ruby sum = 0 for element in arr sum += element end puts sum 25
Idioms in Ruby sum = 0 arr.each do |element| sum += element end puts sum 26
Idioms in Ruby sum = arr.inject(0) do |total, element| total + element end puts sum 27
Idioms in Ruby sum = arr.inject do |total, element| total + element end puts sum 28
Idioms in Ruby puts arr.inject(&:+) 29
Idioms in Ruby puts %w{ Ruby World 松江市 }.inject(&:+) 30
Idioms in Ruby @value ||= calculation(data) @value || (@value = calculation(data)) 31
Idioms in Ruby @cart.calculate_totals if @cart 32
Idioms in Ruby for item in items items.each do |item| process(item) process(item) end end 33
Idioms • Concept compression • Learn them • Create your own 34
Ambiguity 35
Ambiguity • The meaning is not 100% certain • Open to interpretation • “You get to decide” 36
Ambiguity 37
Ambiguity The best poetry—the best art in any medium—is ambiguous. Ambiguity begets participation. Daniel J. Levitin— The World in Six Songs 38
Ambiguity 古池や At the ancient pond 蛙 飛 こむ the frog plunges into the sound of water 水のおと —Sam Hamill 松尾芭蕉 39
Ambiguity 古池や Old pond, 蛙 飛 こむ leap-splash – a frog. 水のおと —Lucien Stryk 松尾芭蕉 40
Ambiguity 古池や Breaking the silence Of an ancient pond, 蛙 飛 こむ A frog jumped into water – 水のおと A deep resonance. —Nobuyuki Yuasa 松尾芭蕉 41
Ambiguity Breaking the silence At the ancient pond Of an ancient pond, the frog plunges into A frog jumped into water – the sound of water A deep resonance. —Sam Hamill —Nobuyuki Yuasa Old pond, leap-splash – a frog. —Lucien Stryk
Ruby and Ambiguity a = 1, a = 2 p a # => [1, 2] 43
Ruby and Ambiguity p a = 1, a = 2 # => 1 2 p a # => 2 44
Ruby and Ambiguity # => 6 Integer(“3”) * 2 Integer (“3”) * 2 # => 33 45
Ruby and Ambiguity "a" "b" # => "ab" %"a" # => "a" %"b" # => "b" %"a" "b" # => "ab” %"a" %"b" # => "a" 46
Ruby and Ambiguity if a = 1 && b = 2 # => 2 p a, b 2 end 47
Ruby and Ambiguity if a = (1 && b = 2) # => 2 p a, b 2 end 48
Ruby and Ambiguity if (a = 1) && (b = 2) p a, b # WARNING! end 49
Is this bad? NO! 50
Without Ambiguity There is no poetry 51
Without Ambiguity There is no magic 52
Without Ambiguity There is no fun 53
And Programming Should be Fun! 54
Images • http://geopolicraticus.wordpress.com • http://www.gamefaqs.com/snes/588787-tetris-attack/images/screen-12 • http://3dsconnect.com/2012/03/02/metal-gear-solid-snake-eater-3d-features-yoshi/ • http://www.free-clipart-pictures.net/object_clipart.html • https://science.nichd.nih.gov/confluence/display/~timrozek/uploads • http://users.humboldt.edu/jwpowell/lwgrip.jpg • http://www.mypi.cn/ • http://www.computerhistory.org/atchm/the-apl-programming-language-source-code/ 55
Recommend
More recommend