Ruby – Como programar, em uma linguagem legal – Apresentação



Ruby – Como programar, em uma linguagem legal – Apresentação

0 0


seti-ufla-2014

Semana de Tecnologia da Informação - SETI UFLA - 2014

On Github jlucasps / seti-ufla-2014

Ruby

Como programar, em uma linguagem legal

Semana de Tecnologia da Informação - SETI UFLA João Lucas P Santana - @jlucasps

Apresentação

  • Ciência da Computação

  • UFLA - Comp 2007/1

  • Fila de carona gigante

  • Elefantinho lotado

  • Brejão

Apresentação

Histórico

“I hope to see Ruby help every programmer in the world to be productive, and to enjoy programming, and to be happy. That is the primary purpose of Ruby language.” Yukihiro “Matz” Matsumoto

Histórico

Feb. 23 1993

"... object-oriented scripting language. I knew Perl (Perl4, not Perl5), but I didn't like it really, because it had smell of toy language (it still has)."

Histórico

"I knew Python then. But I didn't like it, because I didn't think it was a true object-oriented language."

Histórico

"So, I decided to make it." Matz

"It took several months to make the interpreter run. [...] I posted Ruby 0.95 to the Japanese domestic newsgroups in Dec. 1995. " Matz

Everything is a object

4.even?                     # => true
3.upto(8).to_a              # => [3, 4, 5, 6, 7, 8]
"A string literal".length()   # => 16
						

Everything is a object

[].methods
=> [:inspect, :to_s, :to_a, :to_ary, :frozen?, :==, :eql?, :hash, :[],
:[]=, :at, :fetch, :first, :last, :concat, :<<, :push, :pop, :shift,
:unshift, :insert, :each, :each_index, :reverse_each, :length, :size,
:empty?, :find_index, .... ]
						

Everything is a object

10.methods
 => [:to_s, :inspect, :-@, :+, :-, :*, :/, :div, :%, :modulo, :divmod, :fdiv,
 :**, :abs, :magnitude, :==, :===, :<=>, :>, :>=, :<, :<=, :~, :&, :|, :^,
 :[], :<<, :>>, :to_f, :size, :zero?, :odd?, :even?, :succ, :integer?,
 :upto, :downto, :times, :next, :pred, :chr, :ord, :to_i, :to_int, :floor,
 :ceil, :truncate, :round, :gcd, :lcm, :gcdlcm, :numerator, :denominator,
 :to_r, :rationalize, :singleton_method_added, :coerce, :i, :+@, :eql?, ... ]
						

Everything is a object

true.class
 => TrueClass
1.4.class
 => Float
'1+3i'.to_c.class
 => Complex
 '1+3i'.to_c.class.ancestors
 => [Complex, Numeric, Comparable, Object, Kernel, BasicObject]
'1+3i'.to_c.class.class
 => Class

Everything is a object

Ruby code conventions

Nome das classes UpperCamelCase
class InfoCompMagazine
  # ...
end
Métodos e variáveis usam snake_case
def number_of_pages
  # some code here ...
end
def web_magazine?
  # more code ...
end
def change_folder!
  # code code code ...
end

Ruby code conventions

Ruby classes

class Book
  def initialize(title)
    @title = title
  end

  def title
    @title
  end
end

Ruby classes

book = Book.new "Eloquent Ruby"
puts book.title # "Eloquent Ruby"
puts book.send :title # "Eloquent Ruby"
 						
book = Book.new( "Eloquent Ruby" )
puts book.title() # "Eloquent Ruby"
puts book.send( :title ) # "Eloquent Ruby"
 						

Ruby classes

// Java class
public class Person {

  private int age;

  public int getAge() {
       return this.age
  }
  public void setAge(int age) {
       this.age = age;
  }
}
# Ruby class
class Person
  attr_accessor :age
end

Variáveis

class Magazine
  MAX_PAGES = 30 # contants
end
class Magazine
  def initialize
    @max_pages = 30 # instance variable
  end
end
class Magazine
  @@max_pages = 30 # class variable
  def initialize
    # ...
  end
end

Classes abertas

class Fixnum
  def +( num )
    self - num
  end
end
puts 2 + 3
=> -1

Ruby e "Modo Poesia"

Armando Fox, University of California, Berkeley

def print_user_info( info )
  puts "Info about user:"
  info.each_pair { |key, value| puts "#{key}: #{value}" }
end
print_user_info( { name: "Joao lucas", email: "jlucasps@gmail.com" } )
# Info about user:
# name: Joao lucas
# email: jlucasps@gmail.com
 						
print_user_info( name: "Joao lucas", email: "jlucasps@gmail.com" )

print_user_info name: "Joao lucas", email: "jlucasps@gmail.com"
 						

Ruby e "Modo Poesia"

Armando Fox, University of California, Berkeley

user.age.should(be() >= 18)
user.age.should be >= 18
 						
if !logged_in?() # unless logged_in?()
  ( redirect_to( login_page() ) ) and return()
end
# ...
( redirect_to( login_page() ) and return() ) unless logged_in?()
# ...
( redirect_to login_page and return ) unless logged_in?
 						

Metaprogramming

class Car

end
car = Car.new
car.run # undefined method `run' for #<Car:0x00000001c8d970> (NoMethodError)
# singleton method
def car.run
  puts "wooowww Car running now!"
end
car.run # wooowww Car running now!
car2 = Car.new
car2.run # undefined method `run' for #<Car:0x00000002292898> (NoMethodError)

Metaprogramming

class User
  attr_accessor :name, :age, :city
  def initialize name, age, city
    @name, @age, @city = name, age, city
  end
end
user = User.new "João Lucas", 25, "BH"
puts "#{user.name}, #{user.age()} years old, from #{user.city}."
puts user.move( "BH", "POA" )
# Joao Lucas, 25 years old, from BH.
# undefined method `move' for #<User:0x994cee8 @name="João Lucas", @age=25, @city="BH"> (NoMethodError)

Metaprogramming

class User
  def method_missing( method, *args, &block)
    puts "ops.. you can not call #{method}(..) with params #{args.to_a} right now."
    puts "Try again next week! :-D"
  end
end
puts user.move( "BH", "POA" )
# Joao Lucas, 25 years old, from BH.
# ops.. you can not call move(..) with params ["BH", "POA"] right now.
# Try again next week! :-D

Metaprogramming

class User
  MOVE_OPTIONS = [:trip, :travel, :move, :go]

  MOVE_OPTIONS.each do | name |
    define_method( name ) do | *args |
      puts "Method #{name}(..) called with args #{args.inspect}"
    end
  end
end
puts user.move( "BH", "POA" )
# Method move(..) called with args ["BH", "POA"]

Tools, libs, frameworks

github.com/markets/awesome-rubygithub.com/bayandin/awesome-awesomeness

High traffic sites built in Ruby on Rails

quora.com/What-are-some-highly-trafficked-sites-built-in-Ruby-on-Rails

Learn Ruby

Learn Ruby

Ruby

Como programar, em uma linguagem legal

Semana de Tecnologia da Informação - SETI UFLA João Lucas P Santana - @jlucasps

Obrigado!