AIESEC 26 Apr 2013

Post on 31-Oct-2014

230 Views

Category:

Technology

7 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

Transcript

RUBYVlad Verestiuc

http://about.me/vvlad

“My conscience won’t let me call Ruby a computer language. That would imply that the language works primarily on the computer’s terms. That the language is designed to accommodate the computer, first and foremost. “

- Why's (poignant) Guide to Ruby

“Ruby is designed to make programmers happy”

- Matz (Yukihiro Matsumoto)

class OrderItem < ActiveRecord::Base

belongs_to :account belongs_to :order belongs_to :product has_many :categories

validates :name, presence: true validates :quantity, numericality: { greater_than_or_equal_to: 1 } validate :price_value

before_create :ensures_items_are_in_stock after_create :notify_the_account_manager

end

Dar totusi ce este RUBY ?- Este un limbaj de scripting.- Este un limbaj untyped.- Imperativ, OOP, Functional.- Usor extensibil.- Garbage Collected.

Cine il foloseste:- Twitter- Hulu- Groupon- Amazon- Github- 37 Signals- Heroku- Engineyard- Google

Lucruri notabile despre RUBY- Orice este un obiect- Conventii- Obiectele Clase- Module - Blocuri- Orice are o valoare- Orice este un obiect

Conventii:- Variable globale ( incep cu $)- Constante ( incep cu litera mare )- numele functiilor ( numai litere mici, numere , _, =, ? si ! )- variable locale ( numai litere mici, numere )- variable de instanta ( incep cu @, numai litere mici si _)- variable de clasa ( incep cu @@, numai litere mici si _)- functiile care returneaza true sau false ( se termina cu ? )- functiile care modifica starea unui obiect ( se termina cu ! )

Clase:- Mostenire ( simpla si nu numai )- Nu este necesar ca tot codul sa fie definit intrun singur loc. ( Class reopening )- Domenii de vizibiliate: public, private, protected.- Contextul de clasa ( metode de clasa )

class Smurf def walk! start_singing end def start_singing; end end class BabySmurf < Smurf

def walk! take_the_peacekeeper super #se cheama Foo#foo make_noise end def take_the_peacekeeper; end def make_noise; end end

baby = BabySmurf.new baby.walk!

Mostenire

class Beer def open! puts "I how you’ll enjoy it" end end

class Beer def use(compound);end def chilled?; [true,false].sample; end def open! use :liquid_nitrogen unless chilled? puts "Now it is definitely chilled!!! Have a nice drink" end end beer = Beer.new beer.open!

Class reopening

class Human

def access_background; end def solve_issues_for(someone); someone.issues; end def change_underwear_of(someone); someone.underwear; end protected def issues; end def underwear; end private :underwear

end

human, good_friend = Human.new, Human.new human.access_background human.issues # Exceptie human.solve_issues_for good_friend # se executa ok human.change_underwear_of good_friend

Domenii de vizibilitate

class Human

class << self def arise! end end def self.walk! end def Human.kill!(what) end end

Human.arise! Human.walk! Human.kill!(:all)

Contextul de clasa

class Human class << self def belongs_to(something_or_somewhat);end end def self.has(something); end end def Human.acts_like(someone) end

class Man < Human belongs_to :humanity has :more_legs_than_a_woman acts_like :a_moron end Man.acts_like :a_moron_from_time_to_time

Contextul de clasa

class Rabbit

def hole end

def self.foot end

end

SpecialRabbit = Class.new(Rabbit)SpecialRabbit.method(:foot)#<Method: SpecialRabbit(Rabbit).foot>SpecialRabbit.new.method(:hole)#<Method: SpecialRabbit(Rabbit)#hole>

Orice este un obiect !!!

Module:- Colectii de functii- Pot fi incluse in clase.- Pot sa extinda clase.- Se folosesc si pentru a simula mostenirea multipla.

Includere

require 'digest/md5'

module Authenticable def password=(value) @encryped_password = Digest::MD5.hexdigest(value) end def valid_password?(password) @encryped_password == Digest::MD5.hexdigest(password) end end

class User include Authenticable end

user = User.new user.password = 'my_password' user.valid_password? 'my_password' # true user.valid_password? 'other password' # false

Extindere

module Runnable def perform!(id, *arguments) User.find(id).send(*arguments) end def find(id) User.new end end

class User extend Runnable def validations(options={}) end end class AgreeableUnicorn end

User.perform! 10, :validations, with: AgreeableUnicorn.new

Mostenire Multipla

module Mam def nose puts "small" end end module Dad def hair puts "short" end end class Me include Mam include Dad def hair puts "was"; super; puts "but now is long"; end end me = Me.new me.nose me.hair

class Knight def shiny_armour? false endend

module Armour def shiny_armour? true endend

knight = Knight.newknight.shiny_armour?# falseknight.extend Armourknight.shiny_armour?# true

Orice este un obiect !!!

Intrebari ?

3.times { |i| puts "WTF is going on here?!?!" }

winner.each do |winner| winner.award_prize! end 3.times.map { |i| "time-#{i}" } # ["time-0", "time-1", "time-2"]

2.upto(6).map { |i| i } # [2, 3, 4, 5, 6] [1, 2, 3, 4, 5, 6].select do |item| item.odd? end #[1, 3, 5]

Blocuri

Blocuri ca argumente

def each_night(&block) yield :dancing block.call(:sleeping) block["acting as a hash"] end

each_night do |action| puts "I am #{action}" end

def inner_state(number) my_inner_state = if number.odd? "high" else "weird" end "I'm feeling #{my_inner_state}" end puts inner_state(1) #I'm feeling high puts inner_state(2) #I'm feeling weird

Orice are o valoare

Intrebari ?

http://mislav.uniqpath.com/poignant-guide/

http://www.ruby-lang.org/en/documentation/

http://www.confreaks.com/

top related