Slide 1

Slide 1 text

{ title: Ruby, for: "non-rubyists", by: "Tadas Ščerbinskas" }

Slide 2

Slide 2 text

class Ruby def what? "A dynamic, open source programming language " + "with a focus on simplicity and productivity. " + "It has an elegant syntax that is " + "natural to read and easy to write." end end

Slide 3

Slide 3 text

class Ruby def what_for? { web: ["Rails", "Sinatra", "Padrino"], desktop: ["MacRuby", "IronRuby"], mobile: ["RubyMotion", "MobiRuby", "Ruboto"] } end end

Slide 4

Slide 4 text

# Basics ! puts "Hello VilniusRB!" # => nil # >> Hello VilniusRB! ! "Hi".class # => String 1.class # => Fixnum nil.class # => NilClass

Slide 5

Slide 5 text

# Numbers ! -4 # => -4 1.5 # => 1.5 12.3e-3 # => 0.0123 0b101 # => 5 (bin) 0x11 # => 17 (hex) 020 # => 16 (oct) 12_000 # => 12000 10**20 # => 100000000000000000000

Slide 6

Slide 6 text

# Numbers ! -4.class # => Fixnum 1.5.class # => Float 12.3e-3.class # => Float 0b101.class # => Fixnum 0x11.class # => Fixnum 020.class # => Fixnum 12_000.class # => Fixnum (10**20).class # => Bignum

Slide 7

Slide 7 text

# Numbers ! 4.even? # => true 3.zero? # => false 3.next # => 4 ! 3.times do print "Hello " end ! # >> Hello Hello Hello

Slide 8

Slide 8 text

# Numbers ! 2 + 2 # => 4 ! 2.+(2)

Slide 9

Slide 9 text

# Strings and symbols ! "foo #{1 + 2}" # => "foo 3" 'foo #{1 + 2}' # => "foo \#{1 + 2}" ! :foo # => :foo ! "foo".object_id == "foo".object_id # => false :foo.object_id == :foo.object_id # => true

Slide 10

Slide 10 text

# Strings and symbols ! "stressed".reverse # => "desserts" "vilnius".capitalize # => "Vilnius" "patience".length # => 8 " nu ".strip # => "nu" "Lietuva".start_with? "Liet" # => true :ruby.upcase # => :RUBY

Slide 11

Slide 11 text

# Strings and symbols ! "I like coffee".sub("coffee", "beer") # => "I like beer" ! s = "I really really like ruby" s.split.count("really") # => 2

Slide 12

Slide 12 text

# Collections ! [1, 2, 3] # Array ! 1..10 # Range ! { :foo => 11, :bar => 12 } # Hash ! { foo: 11, bar: 12 } # Hash

Slide 13

Slide 13 text

# Collections ! cities = %w[London Oslo Paris Amsterdam Berlin] visited = %w[Berlin Oslo] puts "I still need to visit: ", cities - visited ! # >> I still need to visit: # >> London # >> Paris # >> Amsterdam

Slide 14

Slide 14 text

# Collections ! (1..10).select(&:even?) # => [2, 4, 6, 8, 10] ! ["Tom", "Jerry"].join "&" # => "Tom&Jerry" ! [1, 2, 3].shuffle # => [3, 1, 2] ! [1, 1, 2, 1, 2].uniq # => [1, 2] ! [1, 2, 3].include?(2) # => true

Slide 15

Slide 15 text

# Variables ! monster # is a local variable ! Monster # is a constant ! @monster # is an instance variable ! @@monster # is a class variable ! $monster # is a global variable

Slide 16

Slide 16 text

# Variables ! a, b, c = 1, 2, 3 ! a, b = b, a

Slide 17

Slide 17 text

# Conditionals ! puts "Hello" if monster.has_ears? ! puts "Hello" unless monster.asleep?

Slide 18

Slide 18 text

# Conditionals ! if monster.big? puts "AAaah! run!!" elsif monster.medium? puts "Be careful" else puts "Don't sweat it" end

Slide 19

Slide 19 text

# Conditionals ! case monster.size when :big puts "AAaah! run!!" when :medium puts "Be careful" else puts "Don't sweat it" end

Slide 20

Slide 20 text

# Conditionals ! case monster.size when :big then puts "AAaah! run!!" when :medium then puts "Be careful" else puts "Don't sweat it" end

Slide 21

Slide 21 text

# Blocks ! names = %w[Emily Eva Mia] names.each do |name| puts "Hi #{name}!" end ! # >> Hi Emily! # >> Hi Eva! # >> Hi Mia!

Slide 22

Slide 22 text

# Blocks ! names = %w[Emily Eva Mia] names.each { |name| puts "Hi #{name}!" } ! ! ! # >> Hi Emily! # >> Hi Eva! # >> Hi Mia!

Slide 23

Slide 23 text

# Blocks ! def with_time puts "Started at #{Time.now}" yield puts "Finished at #{Time.now}" end ! with_time { sleep 2 } ! # >> Started at 2014-03-01 00:50:19 +0200 # >> Finished at 2014-03-01 00:50:21 +0200

Slide 24

Slide 24 text

class Monster def initialize(name) @name = name end end

Slide 25

Slide 25 text

module Tail def wiggle puts "wiggling" end end ! class Monster include Tail end ! Monster.new("Swamp Donkey").wiggle

Slide 26

Slide 26 text

# Monkey patching ! 2 + 2 # => 4 ! class Fixnum def +(x) 42 end end ! 2 + 2 # => 42

Slide 27

Slide 27 text

# Refinements ! module Evil refine Fixnum do def +(x) 42 end end end

Slide 28

Slide 28 text

# Refinements ! 2 + 2 # => 4 ! using Evil ! 2 + 2 # => 42

Slide 29

Slide 29 text

# Meta-programming class Shape attr_reader :dimensions ! def initialize(dimensions = {}) @dimensions = dimensions end ! def width dimensions[:width] end ! def height dimensions[:height] end end

Slide 30

Slide 30 text

# Meta-programming class Shape attr_reader :dimensions ! def initialize(dimensions = {}) @dimensions = dimensions end ! [:width, :height].each do |dim| define_method(dim) do dimensions[dim] end end end

Slide 31

Slide 31 text

# Thank you ! ! { name: "Tadas Ščerbinskas", email: "[email protected]", twitter: "tadassce", github: "tadassce" }

Slide 32

Slide 32 text

# Užduotis ! # Sukurkite Template klasę, turinčią body # metodą, kuri grąžins HTML'ą su paduotu # turiniu viduje.

Slide 33

Slide 33 text

references = %w[ https://www.ruby-lang.org http://en.wikipedia.org/wiki/Ruby_(programming_language) https://speakerdeck.com/davidfrancisco/learning-ruby ]