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

The best language I have ever learned.

The best language I have ever learned.

An intro to Ruby

Luís Ferreira

June 27, 2013
Tweet

More Decks by Luís Ferreira

Other Decks in Programming

Transcript

  1. 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!
  2. I wanted a scripting language that was more powerful than

    Perl, and more object-oriented than Python. “ Yukihiru “Matz” Matsumoto creator of Ruby
  3. 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...
  4. 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
  5. // 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
  6. There are ARRAYS arr = [1, “second”, 3.14] There are

    HASHES hash = {1 => “second”} hash[1] And there are SYMBOLS direction = :north
  7. 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.
  8. 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.
  9. 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...
  10. 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:
  11. 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
  12. module ToBeIncluded def foo "bar" end end class MyClass include

    ToBeIncluded end Mixins are modules that are included... ...into classes or other modules
  13. 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
  14. 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__)
  15. 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
  16. 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
  17. 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