The Ruby Language
[@+]pragdave dave@pragprog.com
The Ruby Language [@+]pragdave dave@pragprog.com Ruby A tool for - - PowerPoint PPT Presentation
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
The Ruby Language
[@+]pragdave dave@pragprog.com
Ruby
Ken Iverson
+/⍳5
(~R∊R∘.×R)/R←1⍳R
life←{↑1 ⍵.∧3 4=+/,¯1 0 1∘.⊖¯1 0 1∘.⌽⊂⍵}
5APL
Winner
6Ken Iverson
problems.
Good Notation
7Good Notation
8Ruby
9Ruby
10Ruby and Language
11The limits of my language are the limits of my world
Ludwig Wittgenstein—Logico-Tractatus Philosophicus 12The limits of my language are the limits of my world
Words? Idioms
13meaning
listener
Idiom
14“Yoshi kicked the bucket”
Idiom
15“Yoshi kicked the bucket”
16Pattern
==
Idiom
19==
Pattern
==
development
Idiom
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);
21Idioms 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
22arr = [ 4, 12, 9, 13, 3, 1 ] sum = 0 for i in 0...arr.length sum = sum + arr[i] end puts sum
Idioms in Ruby
23Idioms in Ruby
arr = [ 4, 12, 9, 13, 3, 1 ] sum = 0 for i in 0...arr.length sum += arr[i] end puts sum
24Idioms in Ruby
sum = 0 for element in arr sum += element end puts sum
25Idioms in Ruby
sum = 0 arr.each do |element| sum += element end puts sum
26Idioms in Ruby
sum = arr.inject(0) do |total, element| total + element end puts sum
27Idioms in Ruby
sum = arr.inject do |total, element| total + element end puts sum
28Idioms in Ruby
puts arr.inject(&:+)
29Idioms in Ruby
puts %w{ Ruby World 松江市 }.inject(&:+)
30Idioms in Ruby
@value ||= calculation(data) @value || (@value = calculation(data))
31Idioms in Ruby
@cart.calculate_totals if @cart
32Idioms in Ruby
for item in items process(item) end items.each do |item| process(item) end
33Idioms
Ambiguity
Ambiguity
36Ambiguity
37The best poetry—the best art in any medium—is ambiguous. Ambiguity begets participation.
Daniel J. Levitin—The World in Six SongsAmbiguity
38古池や 蛙飛こむ 水のおと
At the ancient pond the frog plunges into the sound of water —Sam Hamill 松尾芭蕉
Ambiguity
39Old pond, leap-splash – a frog. —Lucien Stryk 松尾芭蕉
Ambiguity
40古池や 蛙飛こむ 水のおと
Breaking the silence Of an ancient pond, A frog jumped into water – A deep resonance. —Nobuyuki Yuasa 松尾芭蕉
Ambiguity
41古池や 蛙飛こむ 水のおと
Ambiguity
At the ancient pond the frog plunges into the sound of water —Sam Hamill Old pond, leap-splash – a frog. —Lucien Stryk Breaking the silence Of an ancient pond, A frog jumped into water – A deep resonance. —Nobuyuki Yuasa
a = 1, a = 2 p a # => [1, 2]
Ruby and Ambiguity
43p a # => 1 2
Ruby and Ambiguity
# => 2 p a = 1, a = 2
44Integer(“3”) * 2 # => 6
Ruby and Ambiguity
Integer (“3”) * 2 # => 33
45Ruby and Ambiguity
"a" "b" # => "ab" %"a" # => "a" %"b" # => "b" %"a" "b" # => "ab” %"a" %"b" # => "a"
46# => 2 2 if a = 1 && b = 2 p a, b end
Ruby and Ambiguity
47# => 2 2 if a = (1 && b = 2) p a, b end
Ruby and Ambiguity
48# WARNING! if (a = 1) && (b = 2) p a, b end
Ruby and Ambiguity
49Is this bad?
50There is no poetry Without Ambiguity
51There is no magic Without Ambiguity
52There is no fun Without Ambiguity
53Images
55