Upgrade to Pro — share decks privately, control downloads, hide ads and more …

#IWDev 6: The Ruby Way - by Brian Prior

Avatar for #IWDev #IWDev
December 04, 2012

#IWDev 6: The Ruby Way - by Brian Prior

Avatar for #IWDev

#IWDev

December 04, 2012
Tweet

More Decks by #IWDev

Other Decks in Programming

Transcript

  1. Origins Conceived 1993 by Yukihiro Matsumoto (‘Matz’) First release 1995

    Became popular about 10 years later due to Ruby on Rails MVC framework
  2. Versions MRI - C coded interpreter (Version 1.8.7 standard) YARV

    - byte code interpreter (faster than MRI). Latest 1.9.3 Other versions developed include: - JRuby (JVM version) - Rubinius - Iron Ruby Ruby 2.0 to be released soon(2013?) and frozen Ruby was accepted as a Japanese Industrial Standard (JIS X 3017) in 2011[8] and an international standard (ISO/IEC 30170) in 2012.
  3. Some general points General purpose language* Almost purely object oriented

    but... ... supports functional and imperative programming paradigms Dynamically typed + ‘duck typed’ (C)lean syntax Fun to use! * Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. (Wikipedia)
  4. What’s a duck type? • “When I see a bird

    that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck”.(James Whitcomb Riley) • A style of dynamic typing in which an object's methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface. • All things are objects in Ruby (see later) - the concept of ‘type’ in the normal sense is not relevant. • Allows polymorphism without inheritance
  5. Availability Runs on all popular platforms (Mac, Windows, Linux) Recommend

    using a Ruby version manager such as RVM or rbenv (github) There is a Ruby installer package for Windows (http://rubyinstaller.org/)
  6. Development Apps & IDEs Any text editor will do Textmate

    very popular but Sublime Text 2 (free and very fast) is also very good (these are text editors) Rubymine (JetBrains) and Aptana Studio (free) - IDEs
  7. Some terminology Ruby’s OO-speak often mentions ‘receivers’ which respond to

    methods (messages). The object that responds to the method (message) is the receiver. ALL methods in Ruby must have an explicit or implicit receiver. The normal notation is: object.method Methods always return an object The interpretation of the message may be different, depending upon the receiver. This is the essence of duck-typing.
  8. Virtually everything in Ruby is an OBJECT • true is

    an object • false is an object • nil is an object • 12 is an object • “ruby” is an object • -1.2345 is an object • Classes are objects
  9. Running Ruby Ruby file extension is .rb e.g. example.rb Run

    from terminal or app (IDE) Running file code: $ ruby filename.rb Interactive ruby: $ irb Alternative to irb is PRY (much better - a Ruby gem) Use ruby command $ruby (don’t bother!) Note : $ represents the command prompt on Mac
  10. Main elements found in Ruby Classes Objects (instances of Classes)

    Modules Reserved words - the core language Blocks Expressions
  11. Some Ruby ‘quirks’ Any public class method can be changed

    in any class Constants start with a capital letter, including Classes and Modules - these are defined as constants Value constants can be changed dynamically but this produces a warning at run time Constants cannot be declared inside methods Only single inheritance allowed but effective multiple inheritance possible with module ‘mixins’ to classes
  12. Ruby Examples • Some simple Ruby examples • Control structures

    • Ruby blocks (closures) • Procs, blocks and lambdas • Special types - indexed collections • Classes, inheritance, modules, gems etc. • BDD example using Rspec gem