the ruby language
play

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


  1. The Ruby Language [@+]pragdave dave@pragprog.com

  2. Ruby • A tool for communication • A tool for exploration • A notation (like mathematics)

  3. 3

  4. Ken Iverson • Creator of APL (1962) • 1979 Turing Award Winner 4

  5. 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

  6. Ken Iverson • Creator of APL (1962) • 1979 Turing Award Winner 6

  7. Good Notation • Ease of expressing constructs arising in problems. • Suggestivity. • Ability to subordinate detail. • Economy. • Amenability to formal proofs. 7

  8. 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

  9. 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

  10. 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

  11. Ruby and Language 11

  12. The limits of my language are the limits of my world Ludwig Wittgenstein— Logico-Tractatus Philosophicus 12

  13. The limits of my language are the limits of my world Words? Idioms 13

  14. Idiom • Words that combine to have special meaning • Meaning shared between speaker and listener 14

  15. Idiom “Yoshi kicked the bucket” 15

  16. “Yoshi kicked the bucket” 16

  17. 横 飯 17

  18. 横飯 18

  19. 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

  20. Idiom == Pattern • Idioms of C++ development • Language-speci fi c 20

  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

  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

  23. 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

  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

  25. Idioms in Ruby sum = 0 for element in arr sum += element end puts sum 25

  26. Idioms in Ruby sum = 0 arr.each do |element| sum += element end puts sum 26

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

  28. Idioms in Ruby sum = arr.inject do |total, element| total + element end puts sum 28

  29. Idioms in Ruby puts arr.inject(&:+) 29

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

  31. Idioms in Ruby @value ||= calculation(data) @value || (@value = calculation(data)) 31

  32. Idioms in Ruby @cart.calculate_totals if @cart 32

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

  34. Idioms • Concept compression • Learn them • Create your own 34

  35. Ambiguity 35

  36. Ambiguity • The meaning is not 100% certain • Open to interpretation • “You get to decide” 36

  37. Ambiguity 37

  38. Ambiguity The best poetry—the best art in any medium—is ambiguous. Ambiguity begets participation. Daniel J. Levitin— The World in Six Songs 38

  39. Ambiguity 古池や At the ancient pond 蛙 飛 こむ the frog plunges into the sound of water 水のおと —Sam Hamill 松尾芭蕉 39

  40. Ambiguity 古池や Old pond, 蛙 飛 こむ leap-splash – a frog. 水のおと —Lucien Stryk 松尾芭蕉 40

  41. Ambiguity 古池や Breaking the silence Of an ancient pond, 蛙 飛 こむ A frog jumped into water – 水のおと A deep resonance. —Nobuyuki Yuasa 松尾芭蕉 41

  42. 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

  43. Ruby and Ambiguity a = 1, a = 2 p a # => [1, 2] 43

  44. Ruby and Ambiguity p a = 1, a = 2 # => 1 2 p a # => 2 44

  45. Ruby and Ambiguity # => 6 Integer(“3”) * 2 Integer (“3”) * 2 # => 33 45

  46. Ruby and Ambiguity "a" "b" # => "ab" %"a" # => "a" %"b" # => "b" %"a" "b" # => "ab” %"a" %"b" # => "a" 46

  47. Ruby and Ambiguity if a = 1 && b = 2 # => 2 p a, b 2 end 47

  48. Ruby and Ambiguity if a = (1 && b = 2) # => 2 p a, b 2 end 48

  49. Ruby and Ambiguity if (a = 1) && (b = 2) p a, b # WARNING! end 49

  50. Is this bad? NO! 50

  51. Without Ambiguity There is no poetry 51

  52. Without Ambiguity There is no magic 52

  53. Without Ambiguity There is no fun 53

  54. And Programming Should be Fun! 54

  55. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend