Click here to load reader

Ruby On Rails - 1. Ruby Introduction

  • View
    123

  • Download
    8

Embed Size (px)

DESCRIPTION

A 90 min introduction on Ruby Programming Language by Christian Feser, Michael Kram, Jakob Schröter and Marc Seeger Contents: Design History of Ruby Agile Manifesto Language basics Exercise Typing Libraries & Gems Ruby VMs Good & bad things

Text of Ruby On Rails - 1. Ruby Introduction

  • 1. Ruby on Rails Web Development that doesnt hurt Dezember 2008www.xing.com/profile/Christian_Feser www.xing.com/profile/Michael_Kram www.xing.com/profile/Jakob_Schroeter www.Marc-Seeger.de

2. Ruby A Programmers Best Friend 3. 3 Christian Feser, Michael Kram, Jakob Schrter, Marc Seegeragenda Design History of Ruby Agile Manifesto Language basics Exercise Typing Libraries & Gems Ruby VMs Good & bad things 4. Design History of Ruby Ruby *is* different from other languages, indeed. --Matz. 5. 5 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerHistory Origin: Yukihiro quot;Matzquot; Matsumoto Japan 1993 1st english book: 2000 Ruby on Rails: 2004 6. 6 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerWhat Matz has to say I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. That's why I decided todesign my own language 7. 7 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerThe Power of Ruby according to Yukihiro Matz Matsumoto 8. 8 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerRuby isSmalltalk Unfamiliar Syntax+ Perls scripting power+ Pythons exceptions etc.+ CLUs iterator 9. 9 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerThis helps making ruby a Scripting Language a dynamic typing Language an Object Oriented Programming Language a good taste of Functional Programming a highly reflective Language a base for creating Domain Specific Languages 10. 10 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerCant I do all of that in Java/C?Sapir-Whorf-Hypothesis: Language determines the way we think Basic Programmers never use recursion LISP programmers use macros for everything FORTRAN programmers can write FORTRAN in any language 11. 11 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerRuby = human-oriented reduces the burden of programming. tries to push jobs back to machines. You can accomplish more tasks with less work in smaller yet readable code. 12. 12Christian Feser, Michael Kram, Jakob Schrter, Marc Seeger principle of least surpriseI believe people want to expressthemselves when they program.They don't want to fight with thelanguage. Programming languages must feel natural to programmers. I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby. 13. 13 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerProgramming experience(according to Dr. Jacob Nielson) Learnability Efficiency Memorability Errors Satisfaction 14. 14 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerHow Ruby helps you: Learnability 15. 15 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerHow Ruby helps you: Efficiency Not that fast to execute BUT fast to programm Pseudo-simplicity Consistency Smartness 16. 16 Christian Feser, Michael Kram, Jakob Schrter, Marc Seeger How Ruby helps you: Memorability Conservativeness helps Easy to remember syntax Ruby is NOT a simple language BUT the complexity is: Hidden Natural Consistent 17. 17 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerHow Ruby helps you: Errors You wont see that many because: Consistent syntax rules less code less bugs 18. 18 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerHow Ruby helps you: Satisfaction Ruby is fun Ruby makes you feel smart 19. 19 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerRuby in five Es Everything is an object Elegant blocks give inline power Exploring with reflection Extending at runtime Extensive standard librayDavid Heinemeier Hansson 20. Agile Manifesto Painless Programming 21. 21 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerRuby An Agile Language? the language design should focus on users the language should encourage readability the language should be expressive, and helpscommunication between developers the language should embrace changes, and bedynamic([email protected]) 22. Language Basics A Programmers Best Friend 23. 23 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerHello World! 24. 24 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerRead the following aloud 25. 25 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: comments 26. 26 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics ClassNames method_names and variable_names methods_asking_a_question? slightly_dangerous_methods! @instance_variables $global_variables SOME_CONSTANTS or OtherConstants 27. 27 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics Variable Declaration: Function Declaration: 28. 28 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Strings 29. 29 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Strings 30. 30 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Strings 31. 31 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Strings 32. 32 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Hashes 33. 33 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Arrays 34. 34 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerGive me some sugar: Array 35. 35 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerControl Structures 36. 36 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Classes 37. 37 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Classes Classes are always open (even built in classes)Another Example from Rails:1.hour.from_now 38. 38 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: Inheritance Single Inheritance But mixins are available (= Interface with implemented methods) 39. 39 Christian Feser, Michael Kram, Jakob Schrter, Marc Seeger Language Basics: Mixin Example This module implements themixin A class that doesnt do that much inheriting and mixing! We inherited that oneAnd mixed in that one 40. 40 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerBlocks Blocks are unnamed functions 41. 41 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerBlocksDefine:Call:Result: 42. 42 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerBlocks Example: IteratorsThe current piece What we are goingof the collection we to do with itare working with 43. 43 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerClosures 44. 44 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLanguage Basics: IO 45. Exercise http://tryruby.hobix.com 46. Typing and why do you call Ruby dynamic? 47. 47 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerTyping: strong / weak Strong typing quot; 5 quot; / 2 NoMethodError Weak typing quot; 16 quot; / 2 8 (e.g. in Perl) Ruby is strongly typed! (Java too) 48. 48 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerTyping: explicit/implicit Explicit: int a = 5 Implicit: a = 5 Ruby is implicitly typed! (Java explicitly) 49. 49 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerTyping: static / dynamic Static typing The compiler checks types during compilation Dynamic typing The compiler doesnt check types during compilation Ruby uses dynamic typing Java uses static typing, C# 4.0 will feature the concept of Dynamic lookup (foreach() already uses it) Visual Basic allows you to do both 50. 50 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerTyping: duck?!If it walks like a duck and quacks like a duck, I would call it a duck. duck.quack Some other languages supporting duck-typing: C# 4.0 will Groovy Javascript Perl Python Smalltalk (no types for variables) duck.walk VB 51. 51 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerTyping: duck Duck typing allows an object to be passed in to amethod that expects a certain type even if itdoesnt inherit from that type. All it has to do issupport the methods and properties of theexpected type in use by the method. 52. Libraries & Gems Because you dont want to do all of the work yourself 53. 53 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerThe ruby world 54. 54 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerLibraries 55. 55 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerRubygems 56. 56 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerGems - search> gem search -l xml *** LOCAL GEMS *** libxml-ruby (0.9.2, 0.9.0, 0.8.3)xml-mapping (0.8.1)xml-object (0.9.8)xml-simple (1.0.11) 57. 57 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerGems - search> gem search -r xml *** REMOTE GEMS *** axml (0.0.2)diff2xml (0.0.2)eimxml (0.0.2)faster_xml_simple (0.5.0)fastxml (0.1.92)gccxml_gem (0.9.1)hashtoxml (0.0.5)jrexml (0.5.3)libxml-feed (0.0.1)libxml-ruby (0.9.4)libxml-xmlrpc (0.1.5)[] 58. 58 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerGems - installing> gem install textgraphSuccessfully installed textgraph-0.1.01 gem installedInstalling ri documentation for textgraph-0.1.0...Installing RDoc documentation for textgraph-0.1.0... 59. 59 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerGems - Updates> gem update Updating installed gemsUpdating hamlSuccessfully installed haml-2.0.5Updating libxml-rubyBuilding native extensions. This could take a while...Successfully installed libxml-ruby-0.9.4-x86- mswin32-60Updating ruby-debug-ideSuccessfully installed ruby-debug-ide-0.4.2[] 60. 60 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerGems - Usage 61. 61 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerRDoc 62. Ruby VMs 63. 63 Christian Feser, Michael Kram, Jakob Schrter, Marc SeegerRuby VMs Ruby 1.8 (MRI-Matz's Ruby Interpreter) Ruby 1.9 (YARV) JRuby Rubinius (IronRuby) 64. 64 Christian Feser, Michael Kram, J