Top Banner
RUBY Vlad Verestiuc http://about.me/vvlad
26

AIESEC 26 Apr 2013

Oct 31, 2014

Download

Technology

Verestiuc Vlad

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: AIESEC 26 Apr 2013

RUBYVlad Verestiuc

http://about.me/vvlad

Page 2: AIESEC 26 Apr 2013

“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)

Page 3: AIESEC 26 Apr 2013
Page 4: AIESEC 26 Apr 2013

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

Page 5: AIESEC 26 Apr 2013

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

Page 6: AIESEC 26 Apr 2013

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

Page 7: AIESEC 26 Apr 2013

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

Page 8: AIESEC 26 Apr 2013

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 ! )

Page 9: AIESEC 26 Apr 2013

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 )

Page 10: AIESEC 26 Apr 2013

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

Page 11: AIESEC 26 Apr 2013

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

Page 12: AIESEC 26 Apr 2013

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

Page 13: AIESEC 26 Apr 2013

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

Page 14: AIESEC 26 Apr 2013

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

Page 15: AIESEC 26 Apr 2013

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 !!!

Page 16: AIESEC 26 Apr 2013

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

Page 17: AIESEC 26 Apr 2013

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

Page 18: AIESEC 26 Apr 2013

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

Page 19: AIESEC 26 Apr 2013

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

Page 20: AIESEC 26 Apr 2013

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 !!!

Page 21: AIESEC 26 Apr 2013

Intrebari ?

Page 22: AIESEC 26 Apr 2013

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

Page 23: AIESEC 26 Apr 2013

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

Page 24: AIESEC 26 Apr 2013

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

Page 25: AIESEC 26 Apr 2013

Intrebari ?

Page 26: AIESEC 26 Apr 2013

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

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

http://www.confreaks.com/