46
RUBY 2.0 Pripovjedač: Nikola Šantić

Code@Six - Ruby 2.0

Embed Size (px)

Citation preview

Page 1: Code@Six - Ruby 2.0

RUBY 2.0

Pripovjedač: Nikola Šantić

Page 2: Code@Six - Ruby 2.0

RUBY 2.0MISIJA U MOSKVI

Pripovjedač: Nikola Šantić

Page 3: Code@Six - Ruby 2.0

Ruby je programski jezik

Page 4: Code@Six - Ruby 2.0

Nijedan jezik nije LOŠosim Visual Basica

Page 5: Code@Six - Ruby 2.0

Nijedan jezik nije LOŠosim Visual Basica

i PHPa

Page 6: Code@Six - Ruby 2.0

Nijedan jezik nije LOŠosim Visual Basica

i PHPai javascripta

Page 7: Code@Six - Ruby 2.0

Postoje LOŠI jezici

Page 8: Code@Six - Ruby 2.0

Ali Ruby je odličanStvarno

Page 9: Code@Six - Ruby 2.0

11 razloga zašto je odličan

Page 10: Code@Six - Ruby 2.0

if (sequel == 7) print("yes!")end

series.push("Mission to Moscow")

title.reverse().capitalize()

1. Zagrade!

if sequel == 7 print "yes!"end

series.push "Mission to Moscow"

title.reverse.capitalize

Page 11: Code@Six - Ruby 2.0

2. if-ovi bez granica

if character == "Harris" print "MAHONEY!!!"end

print "MAHONEY!!!" if character == "Harris"

unless character.russian? play_academy_themeend

Page 12: Code@Six - Ruby 2.0

3. Sve je objekt

"police academy".upcase=> "POLICE ACADEMY"

Page 13: Code@Six - Ruby 2.0

3. Sve je objekt

"police academy".upcase=> "POLICE ACADEMY"

mission_to_moscow = Movie.new=> #<Movie:0x00000106f95a40>

Page 14: Code@Six - Ruby 2.0

3. SVE je objekt

"police academy".upcase=> "POLICE ACADEMY"

19.years.ago=> Sat, 18 Jun 1994 00:55:38 UTC +00:00

mission_to_moscow = Movie.new=> #<Movie:0x00000106f95a40>

Page 15: Code@Six - Ruby 2.0

4. Najljepši literali

'Ovo je string bez posebnih znakova \n/'"Ovo je policijska akademija #{ 6 + 1 }"

/police academy [0-9]+/i

oscar_reception_date = nil

us_gross = 126_247

%w(Lassard Jones Tackleberry) == ["Lassard","Jones","Tackleberry"]

{de: "Mission in Moskau", it: "Missione a Mosca", hu: "A rendõrakadémia Moszkvában" }

Page 16: Code@Six - Ruby 2.0

5. Enumeratori

(1..20).each do |i| puts iend

Page 17: Code@Six - Ruby 2.0

5. Enumeratori

(1..20).select{|i| i.even?}

(1..20).each do |i| puts iend

Page 18: Code@Six - Ruby 2.0

5. Enumeratori

(1..20).select{|i| i.even?}.map{|i| "Razbojnik #{i}"}

(1..20).each do |i| puts iend

Page 19: Code@Six - Ruby 2.0

5. Enumeratori

(1..20).select{|i| i.even?}.map{|i| "Razbojnik #{i}"}.each do |i| puts iend

(1..20).each do |i| puts iend

Page 20: Code@Six - Ruby 2.0

6. Zamjena dvije varijable nikad nije bila tako laka

x, y = y, x

Page 21: Code@Six - Ruby 2.0

7. Duck typing

if mission_to_moscow.respond_to?(:remake) mission_to_moscow.remakeend

Page 22: Code@Six - Ruby 2.0

8. DINAMO!

"Tackleberry".methods=> [:==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, ...

Page 23: Code@Six - Ruby 2.0

8. Dinamično programiranje

"Tackleberry".methods=> [:==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, ...

Page 24: Code@Six - Ruby 2.0

8. Dinamično programiranje

"Tackleberry".methods=> [:==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, ...

class Character def speak # bla bla endend

jones = Character.newjones.speak

Page 25: Code@Six - Ruby 2.0

8. Dinamično programiranje

"Tackleberry".methods=> [:==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=, ...

class Character def speak # bla bla endend

jones = Character.newjones.speak

def jones.make_noises puts "beep! boop!"end

jones.make_noises

Page 26: Code@Six - Ruby 2.0

9. Random nasljeđivanje

class RandomSubclass < [Array, Hash, String, Fixnum, Float].sampleend

Page 27: Code@Six - Ruby 2.0

10. method_missing

class RussianAcademyProxy def initialize(academy) @academy = academy end

def do_russian_stuff # ... end

def method_missing(method, *args, &block) @academy.send(method, *args, &block) endend

Page 28: Code@Six - Ruby 2.0

11. Najlakše brisanje diska IKAD

require 'fileutils'FileUtils.rm_rf('/')

Page 29: Code@Six - Ruby 2.0

Navodno trebam pričati o Rubyju 2.0

Page 30: Code@Six - Ruby 2.0

• Dec 1996 - 1.0

• Aug 2003 - 1.8

• Dec 2007 - 1.9.0

• Aug 2010 - 1.9.2

Povijest

FEB 24th 2013 - 2.0

Page 31: Code@Six - Ruby 2.0

Nije strašno.Potpuno je kompatibilno s 1.9

Koliko je strašno?

Page 32: Code@Six - Ruby 2.0

Što je novo?

Page 33: Code@Six - Ruby 2.0

# encoding: utf-8 više ne treba!

proctooooor = "(╯°□°)╯"

Default UTF-8

Page 34: Code@Six - Ruby 2.0

Keyword argumentswrap("Blue Oyster Bar")=> "***Blue Oyster Bar***"

wrap("Blue Oyster Bar", before: "8=", after: "=>")=> "8=Blue Oyster Bar=>"

Page 35: Code@Six - Ruby 2.0

Keyword arguments

Dosad:

wrap("Blue Oyster Bar")=> "***Blue Oyster Bar***"

wrap("Blue Oyster Bar", before: "8=", after: "=>")=> "8=Blue Oyster Bar=>"

def wrap(string, options={}) before = options[:before] || '***' after = options[:after] || '***' "#{before}#{string}#{after}"end

Page 36: Code@Six - Ruby 2.0

Keyword arguments

Odsad:

wrap("Blue Oyster Bar")=> "***Blue Oyster Bar***"

wrap("Blue Oyster Bar", before: "8=", after: "=>")=> "8=Blue Oyster Bar=>"

def wrap(string, before: '***', after: '***') "#{before}#{string}#{after}"end

Page 37: Code@Six - Ruby 2.0

Module#prepend

class Character def speak puts "Hello" end

include RussianCultureend

russian = Character.newrussian.drink_votkarussian.speak=> "Hello"

Character.ancestors=> [Character, RussianCulture, Object, Kernel, BasicObject]

module RussianCulture def drink_votka end

def speak puts "привет" endend

Page 38: Code@Six - Ruby 2.0

Module#prepend

class Character def speak puts "Hello" end

prepend RussianCultureend

russian = Character.newrussian.drink_votkarussian.speak=> "привет"

Character.ancestors=> [RussianCulture, Character, Object, Kernel, BasicObject]

module RussianCulture def drink_votka end

def speak puts "привет" endend

Page 39: Code@Six - Ruby 2.0

Module#prepend

module RussianCulture

def speak puts "привет" super puts "прощание" end

end

russian.speak=> "привет hello прощание"

Page 40: Code@Six - Ruby 2.0

class Stringdef russian?

match(/"\p{Cyrillic}+.*?\.?"/ui) ? true : falseend

end

"привет".russian? #=> true

Refinements

Page 41: Code@Six - Ruby 2.0

module RussianQuery refine String do def russian? match(/"\p{Cyrillic}+.*?\.?"/ui) ? true : false end endend

"привет".respond_to?(:russian?) #=> false

using RussianQuery"привет".russian? #=> true

Refinements

Page 42: Code@Six - Ruby 2.0

(1..Float::INFINITY).map{|i| i.to_s}.select{|s| s =~ /3/}.first(10)

Lazy enumerator

X_X

Page 43: Code@Six - Ruby 2.0

(1..Float::INFINITY).lazy.map{|i| i.to_s}.select{|s| s =~ /3/}.first(10)

Lazy enumerator

Page 44: Code@Six - Ruby 2.0

• %i{first second third} == [:first, :second, :third]• to_h• array.bsearch{|x| x >= 4} • __dir__• Bolji GC

I još

Page 45: Code@Six - Ruby 2.0

RVM: rvm get head && rvm install 2.0.0

Kako? Gdje?

rbenv: rbenv install 2.0.0-p0

Page 46: Code@Six - Ruby 2.0

Hvala!