ruby crash course
DESCRIPTION
This is an introduction into the Ruby programming language. The presentation took place in Virgo as a crash course into Ruby.TRANSCRIPT
![Page 1: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/1.jpg)
Ruby Crash CourseBácsi László, Virgo Systems
![Page 2: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/2.jpg)
Mi az a Ruby?
Open Source programnyelv
Egy japán csávó csinálja 1993 óta
Objektum orientált
Dinamikus
Gyengén típusos
![Page 3: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/3.jpg)
Objektum orientált
Minden objektum
Nincs többszörös öröklés, helyette Mix-in
Singleton objektumok
Metaprogramozás
![Page 4: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/4.jpg)
Sajátosságok
Beszédes metódusnevek (sort!, exists?)
Könnyen olvasvató szintaxis
Iterátorok ciklusok helyett
Blokkok
![Page 5: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/5.jpg)
Dinamikus
Duck Typing aka Dynamic Typing
Dynamic Dispatch
Dinamikus viselkedés
Reflekció
scope újranyitás, nyílt osztályok
eval
![Page 6: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/6.jpg)
# Mi az `o' objektum osztálya?o = Hash.newo.class # => Hash
# Egy tömb hosszát a `size', vagy `length' metódus adja vissza?array = ['alice', 'bob', 'carol']array.length == array.size # => true# ugyanaz a metódus, össze vannak linkelve
# Mi két tömb különbsége?a2 = ['bob', 'charlie']difference = array - a2 # => ["alice", "carol"]union = array + a2 # => ["alice", "bob", "carol", "bob", "charlie"]real_union = array | a2 # => ["alice", "bob", "carol", "charlie"]
# Hogy hívhatók meg az ismert C függvények?Time.now.strftime('%Y-%m-%d %H:%M') # => "2008-08-12 15:02"
POLSPrinciple of Least Surprise
![Page 7: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/7.jpg)
Kicsit részleteseben
![Page 8: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/8.jpg)
5.times { print "Odelay!" }
Ismerkedés
['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
exit unless "restaurant".include? "aura"
![Page 9: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/9.jpg)
Ismerkedés# számok1, 42, -10000, 3.14, -273.15, 12.043e-04population = 10_041_000
# stringek"negyvenkettő", 'piros', %{akármi}"nem gondha több soros"<<ENDÉs van mégHEREDOCis.END
# szimbólumok (könnyű stringek):a, :b, :white_board
![Page 10: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/10.jpg)
Ismerkedés
# konstansok nagybetűvel kezdődnekRAILS_ENV, Time, Array
# lokális változókx, y, banana, kicsi_piros
# globális változók$x, $!, $&, $LOAD_PATH, $stderr
# példány változók@a, @b, @first_name
# osztály változók@@master_password, @@site
![Page 11: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/11.jpg)
# metódushívás ponttal, vagy osztályon `::'-al is lehet# elhagyható a zárójelpopulation.to_sTime::now
# ? és ! lehet a metódusnév végén0.zero?array.sort!
# lehet láncolnifront_door.paint(3, :red).dry(30).close
Ismerkedés
# blockok5.times { puts "Hello World!" }File.open("readme", "w") do |f| f << "Egy csomó hasznos infó"end[1,3,42,55].sort_by { |x| x % 10 }
![Page 12: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/12.jpg)
# intervallumok(1..100), ('a'..'z'), (0...5)
Ismerkedés
# hashek{:modell => 'Audi A8', :szin => :fekete}{'one' => 'egy', 'two' => 'kettő'}
# tömbök[1, 2, 3], ['egy', 'ketto', 'harom'], %w{egy ketto harom}
# reguláris kifejezések/ruby/, /\d+/, /^[\w-]+$/, %r{\.jpg$}
![Page 13: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/13.jpg)
# operátorok** ! ~ * / % + - &<< >> | ^ > >= < <= <=>|| != =~ !~ && += -= == ===.. ... not and or
Ismerkedés
# kulcsszavakalias and BEGIN begin break case class def defineddo else elsif END end ensure false for ifin module next nil not or redo rescue retryreturn self super then true undef unless until whenwhile yield
![Page 14: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/14.jpg)
plastic_cup = nilplastic_cup.nil? # => trueglass_cup # ~> -:1: undefined local variable or method `glass_cup' for main:Object (NameError)
Nil
if plastic_cup print "Plastic cup is on the up 'n' up!" end
unless plastic_cup print "Plastic cup is on the down low." end
print "Yeah, plastic cup is up again!" if plastic_cupprint "Hardly. It's down." unless plastic_cup
print "We're using plastic 'cause we don't have glass." if plastic_cup unless glass_cup
![Page 15: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/15.jpg)
kitty_toys = [ {:shape => 'sock', :fabric => 'cashmere'}, {:shape => 'mouse', :fabric => 'calico'}, {:shape => 'eggroll', :fabric => 'chenille'}]
next, break
kitty_toys.each do |toy| break if toy[:fabric] == 'chenille' p toyend
non_eggroll = 0kitty_toys.each do |toy| next if toy[:shape] == 'eggroll' non_eggroll = non_eggroll + 1end
![Page 17: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/17.jpg)
def timeline(year) case year when 1983 "Megszülettem" when 1984..1990 "Felhőtlen gyermekkor" when 1990..1996 "Általános iskola Budaörsön" when 1996..2002 "Illyés Gyula Gimnázium szorgos diákja" when 2002..2007 "Vegyesen BME és munka" when 2008..Time.now.year "Virgo" endend
![Page 18: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/18.jpg)
Példák
![Page 19: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/19.jpg)
require 'rubygems'require 'faker'class Person attr_accessor :first_name, :last_name def initialize first, last @first_name, @last_name = first, last end def <=> other last_name <=> other.last_name end def to_s "#{first_name} #{last_name}" end include Comparableendpersons = (1..10).map {Person.new(Faker::Name.first_name, Faker::Name.last_name)}puts persons.sort.map {|p| p.to_s}.join("\n")# prints## Trudie Cremin# Joshua Glover# Abdul Haley# Mortimer Harris# German Hartmann# Aleen Herzog# Adriel Kling# Zack Mann# Dorris McDermott# Cary Satterfield
Mix-in
![Page 20: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/20.jpg)
Array.class # => ClassArray.new
Minden objektum
0.zero? # => true1.zero? # => false1.abs # => 1-1.abs # => 11.methods # => ["%", "inspect", "<<", etc... "[]", "is_a?"]2.+(3) # => 510.class # => Fixnum(10**100).class # => Bignum
a = nila.nil? # => truea.methods # => list of methodsa.abs # => NoMethodError
kivéve változó nevek és blokkok
![Page 21: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/21.jpg)
Majdnem minden üzenet
értékadás és vezér szerkezeteken (if/else, while, etc.) kívül minden
nem metódus hívás, hanem üzenetküldés az objektumnak
![Page 22: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/22.jpg)
string.index("x")# Send :index (with argument "x")string.length# Send :length (with no argument) run_status_reports# Send :run_status_reports (to self) 1 + 2# Send :+ (with argument 2) to the object 1 array[i]# Send :[] (with argument i) to the array
![Page 23: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/23.jpg)
class VCR def initialize @messages = [] end def method_missing(method, *args, &block) @messages << [method, args, block] end def play_back_to(obj) @messages.each do |method, args, block| obj.send(method, *args, &block) end endend
Mi a különbség?
![Page 24: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/24.jpg)
require 'src/vcr'
vcr = VCR.newvcr.sub!(/Java/) { "Ruby" }vcr.upcase!vcr[11,5] = "Universe"vcr << "!"
string = "Hello Java World"puts string
vcr.play_back_to(string)puts string
# OutputHello Java WorldHELLO RUBY Universe!
![Page 25: Ruby Crash Course](https://reader034.vdocuments.site/reader034/viewer/2022051514/54b8faf04a7959e83d8b47cc/html5/thumbnails/25.jpg)
Referenciák
http://ruby-lang.org/
http://ruby-doc.org/
http://poignantguide.net/ruby/
felhasznált prezentációk
http://www.ruby-doc.org/whyruby/