Programmer's Best Friend
Programmer's Best Friend
It’s History
Why We Are Here? Agenda
• To talk about the beautifulness of Ruby.
• “Beauty&The Beast”.
• Start a debate.
• The debate turns to be a fight.
Disappointed?
Can you just tell us what it will be
about?
It’s Agility….
• Some started to import dynamic
languages on top of their VM. Sun invests
in Jython and Jruby.
• With Microsoft on one side and the Java
competitors on the other, each vendor set
out to develop their own dynamic
languages strategy.
Excusa………
• Smalltalk….everything is an object.
• Isn’t java object oriented?
• Ruby inherits this feature from the
Smalltalk.
• Note: sometimes blocks are not objects
Everything Is An Object
IT’s a DUCK
It’s Time for Another Puzzle to Test your
IQ
If I tell you that I have something at home
that does the following:
1. It is an animal.
2. It has two legs.
3. Its name is “batoot”
class Duck
##class body goes here
end
Let the Duck Eat
class Duck
def eat
#method body goes here
end
end
Objects..Initializing Method
class Duck
def initialize
# I will be called
# when a new object of type animal
# is created
end
end
Microsoft has Something to Announce
In my next release.. I am going to support
dynamic typing.
- So you type:
var x = 5 # I get that it’s an integer.
- Instead of
Int x = 5
Congratulations .NET.
Finally you have a dynamic typing.
Ruby has also dynamic typing.
batoot = Duck.new
batoot = Duck.new()
batoot = Duck.new”batoot”
batoot = Duck.new(“batoot”)
Objects..Even After Batoot is Born
class Duck
def eat
end
end
batoot = Duck.new
class Duck
def sleep
end
end
batoot = Duck.new
def batoot.sleep
#method body goes here
end
Objects..Batoot Needs Some Privacy
class Duck
def eat
end
private
def breath
end
end
batoot = Duck.new
batoot.breath # error
batoot.eat # seems correct
class Duck
def fight(something)
if something == @enemy bite
end
private
def bite
end
end
usage:
batoot = Duck.new
batoot.enemy = “ma7zooz”
batoot.enemy = (“ma7zooz”)
batoot.enemy # “ma7zooz”
Hey dude… It’s same as Java….
Objects..More Independent Batoot
We didn’t see everything yet…..
class Duck
attribute_accessor: enemy # read & write
attribute_reader: food # read only
attribute_writer: mood # set only
end
class Animal
def initialize(name)
print name
end
end
class Duck < Animal
def initialize
print “duck’s name is”
super
end
end
duck = Duck.new(“Batoot”) # “duck’s name is Batoot”
Objects..Batoot’s Interfaces
Module CruelBehavior
def attack
bite
end
end
class Duck
include CruelBehavior
end
– dot
– send
– call
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
• 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 isn’t 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 Misèrables
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!
method_added
# called whenever a method is added to a class
# or a module
inherited
# called on a class whenever it is subclassed
included
# called on a module whenever it is included
Java has Reflection……….
So does Ruby
Reflection?
It means the ability to lookup the structure of
the program in run time
Ruby has Reflection……….
You can check the methods of an object
[1,2,3].methods.select{|m|m==”send”}
# [“send”]
or its class (or ancestor class)
32.class # Fixnum
32.Kind_of?Numeric # true
32.instance_of?Fixnum # true
For classes
Duck.superclass #Animal
Duck.ancestors
#returns parent classes and included modules
# [Animal, CruelBehavior, Object, Kernel]
Duck.public_instance_methods
Duck.singelton_methods
# class (static) methods
Duck.constants
Ruby has Reflection…More to come.
For System
ObjectSpace.each_object do |obj|
# iterate over all the objects in the system
end
Java Has Map
import java.util.*;
public class PrintEnv {
public static void main(String[] args) {
Map map = System.getenv();
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry e = (Map.Entry) it.next();
System.out.println(String.format("%s: %s", e.getKey(),
e.getValue()));
}
}
}
Curly braces…
Finding batoot…
batoot = Ducks.select{|duck|duck.name==“batoot”}
Is that it!!!!!!!!!!
Enough is Enough
Readings
• http://poignantguide.net/ruby/
• www.railsenvy.com
• www.rubyonrails.org
• http://oldmoe.blogspot.com
• http://andigutmans.blogspot.com
• Rails for Java Developers