Professional Documents
Culture Documents
Programmer's Best Friend
Programmer's Best Friend
Its History
Disappointed?
Focused on enterprise
integration, transaction
management and other
back-end processing.
Others were considered just
toys.
The killer
Why?
LAMP
Domain Specific to the web.
Fit like a glove to the web paradigm.
We started to hold the market share.
LAMP
Did I forget to say that the successful Yahoo
was Perl?
Then forgive me
PHP packages including Wordpress, Drupal,
mediaWiki, osCommerce, SugarCRM, and
more
My Grandma Taught Me
The tale of Mr. Bangar
LAMP
Communities driven projects.
Users develop the language.
Its Agility.
Excusa
What about the functional programming
languages?
Lisp, Haskell, Erlang..etc
Or you never hear about them?
Say it..say it..say it.
Who Wins?
My Grandma Taught Me
What I should do?
I should know what fit best for my
requirements
Decision!!
Cost?
Stability, reliability, security?
complexity?
Support?
Performance?
Environment?
Released 1995
It was C based single pass interpreter till
version 1. 8 .6
Version 1.9, December 2007, it becomes
virtual machine based.
Many other Implementations exist:
JRuby
IronRuby
Rubinius
ObjectsEverything Is An Object
Smalltalk.everything is an object.
Isnt java object oriented?
Ruby inherits this feature from the
Smalltalk.
Note: sometimes blocks are not objects
Everything Is An Object
A Duck Tale
class Duck
##class body goes here
end
Objects..Initializing Method
class Duck
def initialize
# I will be called
# when a new object of type animal
# is created
end
end
var x = 5
- Instead of
Int x = 5
Congratulations .NET.
Finally you have a dynamic typing.
My Grandma Taught Me
Teenage Shirt
Objects..Birth of Batoot
batoot = Duck.new
batoot = Duck.new()
batoot = Duck.newbatoot
batoot = Duck.new(batoot)
My Grandma Taught Me
The tale of the man & ablutions
When we say it worked with me..
end
#
#
error
seems correct
Objects..Needs Weapons
class Duck
def fight(something)
if something == @enemy bite
end
private
def bite
end
end
Object
Animal
class Animal
def initialize(name)
print name
end
end
class Duck < Animal
def initialize
print ducks name is
super
end
end
duck = Duck.new(Batoot) # ducks name is Batoot
Objects..Batoots Interfaces
Methods..calling
class Duck
def eat(food)
end
end
duck = Duck.new
duck.eat(meshmesh)
duck.send(eat,meshmesh)
eating = cat.method(eat)
eating.call(meshmesh)
Methods
Methods
Class Duck
def eat(*food)
# when variables are received,
# they are stored in an array named food
end
end
batoot = Duck.new
batoot.eat(dust, paper, cucumber)
# or
batoot.send(eat, dust, paper, cucumber)
Methods
Arrays expand to match the arguments
def four(a,b,c,d)
puts #{a}, #{b}, #{c}, #{d}
end
four(1,2,3,4) # => 1,2,3,4
four(1,2,*['a','b']) # => 1,2,a,b
four(*(5..8).to_a) #=> 5,6,7,8
Methods
Default arguments!!!
Return values no need to specify a return
statement. if u left it, ruby will return the last
sentence
I can return multiple values through methods..(
tasty isnt it)
Methods
def min_max(a, b)
return a,b if a < b
b, a
end
min, max = min_max(7,2)
# min now equals 2
# max now equals 7
def swap(a, b)
b, a # similar to saying: a, b = b, a
end
Les Misrables
Our duck Batoot is still feeling miserable,
Everyone is teasing and messing with it.
They call methods not defined for Batoot.
A possible solution is to add a recorder and
see how people with Batoot.
method_missing
This method acts as a trap for undefined methods
Used to create methods on the fly!
class Recorder
@@calls = []
def method_missing(name,*args)
@@calls << [name,args]
end
def play_back(obj)
@@calls.each do |call|
obj.send(call[0], *call[1])
end
end
end
batoot = Duck.new
batoot.capitalize!
batoot.playback
prints Batoot.
inherited
# called on a class whenever it is subclassed
included
# called on a module whenever it is included
So does Ruby
Reflection?
It means the ability to lookup the structure of
the program in run time
# Fixnum
# true
# true
# true
# false
#Animal
Duck.public_instance_methods
Duck.singelton_methods
# class (static) methods
Duck.constants
end
Hashes
Curly braces
{ 'name' => batoot',
location' => fizos home,
status' => single' }
Blocks
Really Powerful...they add a great value of
Functional programming to Ruby.
(which is a characteristic of dynamic languages)
Blocks
Need to Print ducks names
ducks.each{|duck| print duck.name}
#prints batoot, soso, lolo, toto
Finding batoot
batoot = Ducks.select{|duck|duck.name==batoot}
Change the array of ducks to array of ducks names
ducks.collect!{|duck|duck.name}
Blocks
Need to environment variables
ENV.each{|k,v| puts #{k}:#{v}}
Is that it!!!!!!!!!!
Enough is Enough
Readings
Readings
Resources
http://poignantguide.net/ruby/
www.railsenvy.com
www.rubyonrails.org
http://oldmoe.blogspot.com
http://andigutmans.blogspot.com
Rails for Java Developers