38
By Max Titov maxtitov.me Ninja Software Operations Ruby for PHP Developers VS FOR

Ruby for PHP developers

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Ruby for PHP developers

By Max Titovmaxtitov.me

Ninja Software Operations

Ruby for PHPDevelopers VS FOR

Page 2: Ruby for PHP developers

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?

Page 3: Ruby for PHP developers
Page 4: Ruby for PHP developers

Facts

▶ First “Hello World” in 1995 (PHP 1995 too)▶ Opensource (PHP too)▶ Inspired by: Perl, Smalltalk, Lisp, Python …▶ Philosophy: Designed for programmer

productivity and fun.

Page 5: Ruby for PHP developers

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

Page 6: Ruby for PHP developers

Why Ruby?

▶ It’s fun!▶ It’s going to make your better.▶ And definitely it will sabotage what

you believe in.

Page 7: Ruby for PHP developers

Similarities

▶ Ruby has exception handling▶ Garbage collector▶ The is fairly large standard library▶ The are classes and access modifiers

Page 8: Ruby for PHP developers

Ruby is Dynamic

▶ No need to declare variables

var = “World in hell”var.class #String

var = 1var.class #Fixnum

Page 9: Ruby for PHP developers

Ruby is Strong Typed

▶ Like in Java or C# there is no type juggling.You need to convert between types.

a = “1”b = 2a + b #TypeError: can't convert Fixnum into String

a.to_i + b # 3

Page 10: Ruby for PHP developers

Everything is an Object

▶ Inspired by SmallTalk▶ Unlike other programming languages

that states the same, Ruby really is.

Page 11: Ruby for PHP developers

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)…

Page 12: Ruby for PHP developers

Everything is an Object

▶ Control structures are object methods

class Fixnum < Integerdef – numeric

# subtracting codeend

end

Page 13: Ruby for PHP developers

Ruby is Flexible

▶ Existing ruby code could be easily altered.

class Numeric def toSquare

self * selfend

end

2.toSquare# 4

Page 14: Ruby for PHP developers

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)

Page 15: Ruby for PHP developers

Duck typing

What makes object an object?

Answer is a:

Behavior

Page 16: Ruby for PHP developers

So, is it a duck?

Swim? YesCan Quack? Yes

Is it a duck?Definitely!

Page 17: Ruby for PHP developers

And this?

Swim? YesCan Quack? Yes. Kind of strange, but still it make quack like sound

Is it a duck?Looks like!

Page 18: Ruby for PHP developers

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!

Page 19: Ruby for PHP developers

Or, probably this?

Swim? YepCan quack? Can make weird quacksounds.

Is it duck?Trying very hard, soyes

Page 20: Ruby for PHP developers

Duck Typing

▶ So, everything that could respond to several criteria's that makes us believethat it’s a duck, is a duck.

Page 21: Ruby for PHP developers

Duck Typing in context of Ruby

▶ There is no abstract classes and interfaces.

▶ There is Modules and Mixins.

Page 22: Ruby for PHP developers

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

Page 23: Ruby for PHP developers

How we usually do this in PHP

Interface ILog{

function write($message)}

EventLog implements ILog{

function write($message){//useful code}

}

Page 24: Ruby for PHP developers

How we do this in Ruby

module Log def write

#code endEnd

class EventLog include Log def Prepare endend

Page 25: Ruby for PHP developers

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”, whichyields successive members of the collection.

Page 26: Ruby for PHP developers

Implementing Enumerable

class MyCollection include Enumerable def each

#yields result endend

Page 27: Ruby for PHP developers

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

Page 28: Ruby for PHP developers

You. When you get someone's code with different coding guide lines.

Page 29: Ruby for PHP developers

Ruby Coding guide lines

Ruby syntaxes mostly dictates coding guidelines:

▶ localVariable▶ @instanceVariable▶ @@classVariable▶ $globalVariable▶ Constant ▶ ClassName▶ method_name

Page 30: Ruby for PHP developers

Ruby metaprogramming

▶ DRY – Don’t repeat yourself.▶ But that’s another story

Page 31: Ruby for PHP developers

Frameworks

Ruby

▶ Ruby on Rails, Merb

▶ Sinatra▶ Radiant,

Mephisto

PHP

▶ Symfony, Yii, Zend …

▶ Sylex▶ WordPress,

Drupal, Joomla

Page 32: Ruby for PHP developers

Tools

Ruby

▶ Ruby Gems ▶ Bundler ▶ TestUnit,

minitest▶ Cucumber,

Rspec, Shoulda

PHP

▶ PEAR, Composer▶ Bash, Composer▶ PHPUnit ▶ Behat

Page 33: Ruby for PHP developers

Testing Rocks!

Page 34: Ruby for PHP developers

Feel like a Rubier now?

Page 35: Ruby for PHP developers

Ruby tutorial 101

Interactive ruby tutorial:▶ http://tryruby.org/

Online course:▶ http://www.coursera.org/course/saas/

Page 36: Ruby for PHP developers

Books

▶Programming Ruby (Pick Axe book)By Thomas D., Fowler C., Hunt A.

▶Design Patterns In RubyBy Russ Olsen

▶Search Google for: Learn Ruby

Page 37: Ruby for PHP developers

Follow the ruby side we have

cookies

Page 38: Ruby for PHP developers

Questions?Ruby for PHP developers

By Max TitovGet examples: www.maxtitov.meGet in touch: [email protected]

Twitter: eolexe