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

Ruby for PHP developers

Max Titov
November 22, 2012

Ruby for PHP developers

Introduction to Ruby for PHP developers

Max Titov

November 22, 2012
Tweet

More Decks by Max Titov

Other Decks in Programming

Transcript

  1. Objective: learn and compare ▶  What is Ruby and where

    it is come from? ▶  Why Ruby? ▶  Ruby basics ▶  Ruby ecosystem ▶  Ruby specialties ▶  How to get started?
  2. Facts ▶  First  “Hello  World”  in  1995  (PHP  1995  too)

      ▶  Opensource  (PHP  too)   ▶  Inspired  by:  Perl,  Smalltalk,  Lisp,  Python  …   ▶  Philosophy:  Designed  for  programmer     producIvity  and  fun.  
  3. Creator "I wanted a scripting language that was more powerful

    than Perl, and more object- oriented than Python. That's why I decided to design my own language.”      Yukihiro  (Matz)  Matsumoto
  4. Why Ruby? ▶  It’s fun! ▶  It’s going to make

    your better. ▶  And definitely it will sabotage what you believe in.
  5. Similarities ▶  Ruby has exception handling ▶  Garbage collector ▶ 

    The is fairly large standard library ▶  The are classes and access modifiers
  6. Ruby is Dynamic ▶  No need to declare variables var

    = “World in hell” var.class #String var = 1 var.class #Fixnum
  7. Ruby is Strong Typed ▶  Like in Java or C#

    there is no type juggling.
 You need to convert between types. a = “1” b = 2 a + b #TypeError: can't convert Fixnum into String a.to_i + b # 3
  8. Everything is an Object ▶  Inspired by SmallTalk ▶  Unlike

    other programming languages that states the same, Ruby really is.
  9. Everything is an Object ▶  Primitive Types are an objects

    
 10.times {puts “I am sexy and I know it!”}
 #Output “I am sexy and I know it!”
 #Output “I am sexy and I know it!”
 #Output “I am sexy and I know it!”
 #Output “I am sexy and I know it!”
 #Output “I am sexy and I know it!”
 #....(10 times)…
  10. Everything is an Object ▶  Control structures are object methods

    class Fixnum < Integer def – numeric # subtracting code end end
  11. Ruby is Flexible ▶  Existing ruby code could be easily

    altered. class Numeric def toSquare self * self end end 2.toSquare# 4
  12. Duck typing ▶  Definition: 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. (Wikipedia)
  13. So, is it a duck? Swim?  Yes   Can  Quack?

     Yes     Is  it  a  duck?   Definitely!  
  14. And this? Swim?  Yes   Can  Quack?  Yes.  Kind  of

      strange,  but  sIll  it     make  quack  like  sound     Is  it  a  duck?   Looks  like!  
  15. How, about this? Swim?  Badly,  but  yes.     Can

     Quack?  Yeah,  make     Plenty  of  sounds  but,  can     quack  also.     Is  it  a  duck?   Sort  of  weird  duck,  but  yes!  
  16. Or, probably this? Swim?  Yep   Can  quack?  Can  

      make  weird  quack   sounds.     Is  it  duck?   Trying  very  hard,  so   yes  J  
  17. Duck Typing ▶  So, everything that could respond to several

    criteria's that makes us believe
 that it’s a duck, is a duck.
  18. Duck Typing in context of Ruby ▶  There is no

    abstract classes and interfaces. ▶  There is Modules and Mixins.
  19. Modules and Mixins ▶  Modules define reusable pieces of code

    that couldn’t be instantiated. ▶  Modules provides a namespace and prevent name clashes ▶  Modules could be “mixin” to any class that satisfy conventions described in documentation (Should quack and swim like a duck). ▶  In PHP 5.4 Traits is an equivalent to Mixins
  20. How we usually do this in PHP Interface ILog {

    function write($message) } EventLog implements ILog { function write($message) { //useful code } }
  21. How we do this in Ruby module Log def write

    #code end End class EventLog include Log def Prepare end end
  22. Implementing Enumerable ▶  From Enumerable module documentation:
 The Enumerable mixin

    provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method “each”, which
 yields successive members of the collection.
  23. About coding guide lines ▶  Remember the times of Hungarian

    notation? $f_amount = 100.00; $s_string = “I am definitely a string”; ▶  How many coding guide lines there? ▶  PEAR, ▶  Zend, ▶  Wordpress ▶  Your company standard
  24. Ruby Coding guide lines Ruby syntaxes mostly dictates coding guidelines:

    ▶  localVariable ▶  @instanceVariable ▶  @@classVariable ▶  $globalVariable ▶  Constant ▶  ClassName ▶  method_name
  25. Frameworks Ruby ▶  Ruby on Rails, Merb ▶  Sinatra ▶ 

    Radiant, Mephisto PHP ▶  Symfony, Yii, Zend … ▶  Sylex ▶  WordPress, Drupal, Joomla
  26. Tools Ruby ▶  Ruby Gems ▶  Bundler ▶  TestUnit, minitest

    ▶  Cucumber, Rspec, Shoulda PHP ▶  PEAR, Composer ▶  Bash, Composer ▶  PHPUnit ▶  Behat
  27. Books ▶  Programming Ruby (Pick Axe book)
 By Thomas D.,

    Fowler C., Hunt A. ▶  Design Patterns In Ruby
 By Russ Olsen ▶  Search Google for: Learn Ruby