SlideShare a Scribd company logo
RUBY
Vlad 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)
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
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
end
end
module Armour
def shiny_armour?
true
end
end
knight = Knight.new
knight.shiny_armour?
# false
knight.extend Armour
knight.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/

More Related Content

PPTX
How toextreme
PPT
Ch08 slides
PPT
Pecha Kucha
PPT
Taylor N
PDF
Fluid, Fluent APIs
PDF
Culture And Aesthetic Revisited
PDF
Ruby seen by a C# developer
How toextreme
Ch08 slides
Pecha Kucha
Taylor N
Fluid, Fluent APIs
Culture And Aesthetic Revisited
Ruby seen by a C# developer

Similar to AIESEC 26 Apr 2013 (20)

PDF
Ruby seen from a C# developer
KEY
Language supports it
PDF
Boxen: How to Manage an Army of Laptops and Live to Talk About It
PDF
ORUG - Sept 2014 - Lesson When Learning Ruby/Rails
PDF
Ruby tutorial
PDF
festival ICT 2013: Ruby, the 0.8 language you were looking for
PDF
Scottish Ruby Conference 2014
PDF
Building an Ecosystem for Hackers
PDF
Metaprogramming
PDF
PHP Doesn't Suck
PDF
&lt;img src="../i/r_14.png" />
PDF
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
PDF
in-memory capacity planning, Erlang Factory London 09
PPTX
Ruby for .NET developers
PDF
Reverse Engineering in Linux - The tools showcase
KEY
Introduction to Ruby
PDF
The Joy Of Ruby
KEY
Rails console
PDF
There Are Fates Worse Than Death: The OPW2013 Keynote
PPTX
Apex for humans
Ruby seen from a C# developer
Language supports it
Boxen: How to Manage an Army of Laptops and Live to Talk About It
ORUG - Sept 2014 - Lesson When Learning Ruby/Rails
Ruby tutorial
festival ICT 2013: Ruby, the 0.8 language you were looking for
Scottish Ruby Conference 2014
Building an Ecosystem for Hackers
Metaprogramming
PHP Doesn't Suck
&lt;img src="../i/r_14.png" />
&lt;b>PHP&lt;/b> Reference: Beginner to Intermediate &lt;b>PHP5&lt;/b>
in-memory capacity planning, Erlang Factory London 09
Ruby for .NET developers
Reverse Engineering in Linux - The tools showcase
Introduction to Ruby
The Joy Of Ruby
Rails console
There Are Fates Worse Than Death: The OPW2013 Keynote
Apex for humans
Ad

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Approach and Philosophy of On baking technology
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
A Presentation on Touch Screen Technology
PDF
project resource management chapter-09.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
August Patch Tuesday
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
cloud_computing_Infrastucture_as_cloud_p
A Presentation on Artificial Intelligence
Web App vs Mobile App What Should You Build First.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation_ Review paper, used for researhc scholars
Heart disease approach using modified random forest and particle swarm optimi...
Approach and Philosophy of On baking technology
gpt5_lecture_notes_comprehensive_20250812015547.pdf
MIND Revenue Release Quarter 2 2025 Press Release
WOOl fibre morphology and structure.pdf for textiles
NewMind AI Weekly Chronicles - August'25-Week II
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
A Presentation on Touch Screen Technology
project resource management chapter-09.pdf
Programs and apps: productivity, graphics, security and other tools
August Patch Tuesday
Zenith AI: Advanced Artificial Intelligence
cloud_computing_Infrastucture_as_cloud_p
Ad

AIESEC 26 Apr 2013

  • 2. “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)
  • 4. 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
  • 5. Dar totusi ce este RUBY ? - Este un limbaj de scripting. - Este un limbaj untyped. - Imperativ, OOP, Functional. - Usor extensibil. - Garbage Collected.
  • 6. Cine il foloseste: - Twitter - Hulu - Groupon - Amazon - Github - 37 Signals - Heroku - Engineyard - Google
  • 7. Lucruri notabile despre RUBY - Orice este un obiect - Conventii - Obiectele Clase - Module - Blocuri - Orice are o valoare - Orice este un obiect
  • 8. 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 ! )
  • 9. 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 )
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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 !!!
  • 16. Module: - Colectii de functii - Pot fi incluse in clase. - Pot sa extinda clase. - Se folosesc si pentru a simula mostenirea multipla.
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. class Knight def shiny_armour? false end end module Armour def shiny_armour? true end end knight = Knight.new knight.shiny_armour? # false knight.extend Armour knight.shiny_armour? # true Orice este un obiect !!!
  • 22. 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
  • 23. 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
  • 24. 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