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

Miért szeretjük a Ruby-t?

Miért szeretjük a Ruby-t?

The first ever budapest.rb presentation about why we love Ruby.

László Bácsi

September 17, 2008
Tweet

More Decks by László Bácsi

Other Decks in Programming

Transcript

  1. Mi az a Ruby? Open Source programnyelv Egy japán csávó

    csinálja 1993 óta Objektumorientált Dinamikus
  2. Olvasható kód 5.times { puts "budapest.rb is here" } ['toast',

    'cheese', 'wine'].each do |food| print food.capitalize end exit unless "restaurant".include? "aura"
  3. "ojjektum".class # => String :key.class # => Symbol { :event

    => "budapest.rb", 'event-at' => Time.now }.class # => Hash 1.class # => Fixnum 2_000_000_000.class # => Bignum [1, 5, 42].class # => Array Array.class # => Class Class.class # => Class Minden objektum
  4. # iterálásra (ciklusok helyett) 5.times { puts "Hello World!" }

    # erőforráskezelésre, tranzakciókhoz File.open("readme", "w") do |f| f << "Egy csomó hasznos infó" end # metódus viselkedésének meghatározására [1,3,42,55].sort_by { |x| x % 10 } Utasításblokkok
  5. s = "blue" def s.ish self + "ish" end s.ish

    # => "blueish" s.capitalize.ish # ~> undefined method `ish' for "Blue":String (NoMethodError) "red".ish # ~> undefined method `ish' for "red":String (NoMethodError) Singleton metódusok
  6. class User attr_accessor :name def initialize name @name = name

    end include Comparable def <=> other self.name <=> other.name end end users = %w{Bob Carol Eve Mallory Alice Dave}.map {|n| User.new(n)} users.sort.map {|u| u.name} # => ["Alice", "Bob", "Carol", "Dave", "Eve", "Mallory"] alice = User.new("Alice") bob = User.new("Bob") bob > alice # => true Mix-inek
  7. Fixnum.class_eval do define_method :to_binary do self.to_s(2) end end 42.to_binary #

    => "101010" # osztály újranyitással class Fixnum def to_octal self.to_s(8) end end 42.to_octal # => "52" Metaprogramozás
  8. class Duck def hap_hap puts "háp háp" end end class

    Hunter def hap_hap puts "haphap" end end duck = Duck.new; hunter = Hunter.new duck.respond_to?(:hap_hap) # => true hunter.respond_to?(:hap_hap) # => true gun.shoot(hunter) Duck typing “If it walks like a duck and quacks like a duck, I would call it a duck.“
  9. "".methods # => ["upcase!", "zip", "find_index", "between?", "unpack", ...] Fixnum.ancestors

    # => [Fixnum, Integer, Precision, Numeric, Comparable, Object, Kernel] User.new("Dave").instance_variables # => ["@name"] require 'net/http' Net::HTTP.constants # => ["HTTPVersion", "Get", "Lock", "Post", "Put", "Head", ...] Net::HTTP.singleton_methods # => ["version_1_1?", "socket_type", "start", ...] Reflekció