The Ruby Language [@+]pragdave dave@pragprog.com Ruby A tool for - - PowerPoint PPT Presentation

the ruby language
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

The Ruby Language

[@+]pragdave dave@pragprog.com

slide-2
SLIDE 2

Ruby

  • A tool for communication
  • A tool for exploration
  • A notation (like mathematics)
slide-3
SLIDE 3 3
slide-4
SLIDE 4
  • Creator of APL (1962)
  • 1979 Turing Award Winner
4

Ken Iverson

slide-5
SLIDE 5
  • 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

APL

slide-6
SLIDE 6
  • Creator of APL (1962)
  • 1979 Turing Award

Winner

6

Ken Iverson

slide-7
SLIDE 7
  • Ease of expressing constructs arising in

problems.

  • Suggestivity.
  • Ability to subordinate detail.
  • Economy.
  • Amenability to formal proofs.

Good Notation

7
slide-8
SLIDE 8
  • Code close to the problem.
  • Help you discover new insights.
  • Build layers of abstraction.
  • Small set of basic concepts.
  • Amenability to formal proofs.

Good Notation

8
slide-9
SLIDE 9
  • Code close to the problem.
  • Help you discover new insights.
  • Build layers of abstraction.
  • Small set of basic concepts.
  • Easy to prove code correct.

Ruby

9
slide-10
SLIDE 10
  • Code close to the problem.
  • Help you discover new insights.
  • Build layers of abstraction.
  • Small set of basic concepts.
  • Easy to make code correct.

Ruby

10
slide-11
SLIDE 11

Ruby and Language

11
slide-12
SLIDE 12

The limits of my language are the limits of my world

Ludwig Wittgenstein—Logico-Tractatus Philosophicus 12
slide-13
SLIDE 13

The limits of my language are the limits of my world

Words? Idioms

13
slide-14
SLIDE 14
  • Words that combine to have special

meaning

  • Meaning shared between speaker and

listener

Idiom

14
slide-15
SLIDE 15

“Yoshi kicked the bucket”

Idiom

15
slide-16
SLIDE 16

“Yoshi kicked the bucket”

16
slide-17
SLIDE 17

横 飯

17
slide-18
SLIDE 18

横飯

18
slide-19
SLIDE 19
  • Small set of words with very deep meaning
  • Very efficient way of communicating
  • Difference between stranger and native user

Pattern

==

Idiom

19

==

slide-20
SLIDE 20

Pattern

==

  • Idioms of C++

development

  • Language-specific
20

Idiom

slide-21
SLIDE 21

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
slide-22
SLIDE 22

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
slide-23
SLIDE 23

arr = [ 4, 12, 9, 13, 3, 1 ] sum = 0 for i in 0...arr.length sum = sum + arr[i] end puts sum

Idioms in Ruby

23
slide-24
SLIDE 24

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
slide-25
SLIDE 25

Idioms in Ruby

sum = 0 for element in arr sum += element end puts sum

25
slide-26
SLIDE 26

Idioms in Ruby

sum = 0 arr.each do |element| sum += element end puts sum

26
slide-27
SLIDE 27

Idioms in Ruby

sum = arr.inject(0) do |total, element| total + element end puts sum

27
slide-28
SLIDE 28

Idioms in Ruby

sum = arr.inject do |total, element| total + element end puts sum

28
slide-29
SLIDE 29

Idioms in Ruby

puts arr.inject(&:+)

29
slide-30
SLIDE 30

Idioms in Ruby

puts %w{ Ruby World 松江市 }.inject(&:+)

30
slide-31
SLIDE 31

Idioms in Ruby

@value ||= calculation(data) @value || (@value = calculation(data))

31
slide-32
SLIDE 32

Idioms in Ruby

@cart.calculate_totals if @cart

32
slide-33
SLIDE 33

Idioms in Ruby

for item in items process(item) end items.each do |item| process(item) end

33
slide-34
SLIDE 34

Idioms

  • Concept compression
  • Learn them
  • Create your own
34
slide-35
SLIDE 35 35

Ambiguity

slide-36
SLIDE 36
  • The meaning is not 100% certain
  • Open to interpretation
  • “You get to decide”

Ambiguity

36
slide-37
SLIDE 37

Ambiguity

37
slide-38
SLIDE 38

The best poetry—the best art in any medium—is ambiguous. Ambiguity begets participation.

Daniel J. Levitin—The World in Six Songs

Ambiguity

38
slide-39
SLIDE 39

古池や 蛙飛こむ 水のおと

At the ancient pond the frog plunges into the sound of water —Sam Hamill 松尾芭蕉

Ambiguity

39
slide-40
SLIDE 40

Old pond, leap-splash – a frog. —Lucien Stryk 松尾芭蕉

Ambiguity

40

古池や 蛙飛こむ 水のおと

slide-41
SLIDE 41

Breaking the silence Of an ancient pond, A frog jumped into water – A deep resonance. —Nobuyuki Yuasa 松尾芭蕉

Ambiguity

41

古池や 蛙飛こむ 水のおと

slide-42
SLIDE 42

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

slide-43
SLIDE 43

a = 1, a = 2 p a # => [1, 2]

Ruby and Ambiguity

43
slide-44
SLIDE 44

p a # => 1 2

Ruby and Ambiguity

# => 2 p a = 1, a = 2

44
slide-45
SLIDE 45

Integer(“3”) * 2 # => 6

Ruby and Ambiguity

Integer (“3”) * 2 # => 33

45
slide-46
SLIDE 46

Ruby and Ambiguity

"a" "b" # => "ab" %"a" # => "a" %"b" # => "b" %"a" "b" # => "ab” %"a" %"b" # => "a"

46
slide-47
SLIDE 47

# => 2 2 if a = 1 && b = 2 p a, b end

Ruby and Ambiguity

47
slide-48
SLIDE 48

# => 2 2 if a = (1 && b = 2) p a, b end

Ruby and Ambiguity

48
slide-49
SLIDE 49

# WARNING! if (a = 1) && (b = 2) p a, b end

Ruby and Ambiguity

49
slide-50
SLIDE 50

NO!

Is this bad?

50
slide-51
SLIDE 51

There is no poetry Without Ambiguity

51
slide-52
SLIDE 52

There is no magic Without Ambiguity

52
slide-53
SLIDE 53

There is no fun Without Ambiguity

53
slide-54
SLIDE 54

And Programming Should be Fun!

54
slide-55
SLIDE 55
  • 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/

Images

55