Slide 1

Slide 1 text

The best language I have ever learned

Slide 2

Slide 2 text

Is the best a reachable goal?

Slide 3

Slide 3 text

Know your limitations. Choose what’s best for the problem.

Slide 4

Slide 4 text

My problem was the WEB. My solution was RUBY.

Slide 5

Slide 5 text

@zamith

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

WHAT MAKES RUBY GREAT IS that it’s REALLY Object Oriented, dynamically typed, for me and most of all it’s optimized for the programmer’s productivity and happiness!

Slide 8

Slide 8 text

I wanted a scripting language that was more powerful than Perl, and more object-oriented than Python. “ Yukihiru “Matz” Matsumoto creator of Ruby

Slide 9

Slide 9 text

GETTING DOWN TO THE BASICS

Slide 10

Slide 10 text

def hello_world() return "Hello world!" end hello_world() This is a valid method, def hello_world "Hello world!" end hello_world but this is too...

Slide 11

Slide 11 text

class Lecture def initialize(name = "TBA", duration_in_min = 30) @name = name @duration = duration_in_min/60.0 end def description "Name: #{@name}\nDuration: #{@duration} hours" end end lecture = Lecture.new("Ruby", 45) lecture.description It’s object oriented

Slide 12

Slide 12 text

// Java maximum = Math.max(1, 3) # Ruby maximum = [1, 3].max # Python positive = abs(num) # Ruby positive = num.abs // C length = strlen(name) # Ruby length = name.length It’s REALLY object oriented

Slide 13

Slide 13 text

There are ARRAYS arr = [1, “second”, 3.14] There are HASHES hash = {1 => “second”} hash[1] And there are SYMBOLS direction = :north

Slide 14

Slide 14 text

BLOCKS

Slide 15

Slide 15 text

sum = 0 (1..5).each do |n| # same as [1,2,3,4,5] sum += n end This is a block... ... used as an iterator.

Slide 16

Slide 16 text

They are basically closures, that can be stored in variables and called anywhere. They can use variables from the surrounding scope. In Ruby you have blocks, Procs and lambdas which are all similar.

Slide 17

Slide 17 text

ITERATORS

Slide 18

Slide 18 text

In Ruby the iterator is internal to the collection, unlike Java. It’s just a method that calls yield every time it generates a new value. Ruby already provides a lot of useful iterators: each, map, inject, etc...

Slide 19

Slide 19 text

def fib(max) i1, i2 = 1, 1 # parallel assignment while i1 <= max yield i1 i1, i2 = i2, i1+i2 end end fib(1337) { |n| print n } You can build your own:

Slide 20

Slide 20 text

SHARING FUNCTIONALITY

Slide 21

Slide 21 text

class SuperClass def hello_world "Hello world!" end end class SubClass < SuperClass end superclass = SuperClass.new subclass = SubClass.new Single inheritance Built-in functionality comes from the Object class

Slide 22

Slide 22 text

module ToBeIncluded def foo "bar" end end class MyClass include ToBeIncluded end Mixins are modules that are included... ...into classes or other modules

Slide 23

Slide 23 text

When to use them? You should evaluate the relation between the objects. a is a b, use inheritance a has a b, use composition a behaves like b, use mixins

Slide 24

Slide 24 text

REFLECTION

Slide 25

Slide 25 text

Reflection is the ability to examine an object at runtime.

Slide 26

Slide 26 text

You can transverse all living objects ObjectSpace.each_object { |x| puts x } You can look inside objects [1,2,3].methods [1,2,3].respond_to?(:to_s) [1,2,3].kind_of?(Hash) You can invoke methods without knowing their names a = [1,2,3] a.send(a.private_methods.first.to_sym) You can know about the runtime environment Fixnum.class_variables print File.read(__FILE__)

Slide 27

Slide 27 text

METAPROGRAMMING

Slide 28

Slide 28 text

The writing of programs that write or manipulate other programs (or themselves).

Slide 29

Slide 29 text

Ruby allows you to define methods at runtime class BabyInfo ["cry", "eat"].each do |baby_action| define_method(baby_action) do "Of course, babies #{baby_action}" end end end

Slide 30

Slide 30 text

There are some callbacks you can use class BabyInfo ["cry", "eat"].each do |baby_action| define_method(baby_action) do "Of course, babies #{baby_action}" end end def method_missing(name, *args, &blk) "Nope, babies don’t #{name}" end end

Slide 31

Slide 31 text

There are many others, such as method_added, included, inherited... Metaprogramming in Ruby is a very powerful tool and therefore must be used with care. You can even modify String, Float or even Class and Object

Slide 32

Slide 32 text

LEARN RUBY

Slide 33

Slide 33 text

THANKS and special thanks to Gonçalo Silva

Slide 34

Slide 34 text

@ZAMITH blog.zamith.pt