Top Banner
Vilniaus Ruby bendruomenė Ruby.new Vidmantas Kabošis @ VilniusRB.new 2014-11-22
67

Ruby.new @ VilniusRB

Jul 02, 2015

Download

Software

Introduction to Ruby programming language
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: Ruby.new @ VilniusRB

Vilniaus Ruby bendruomenė

Ruby.newVidmantas Kabošis @ VilniusRB.new

2014-11-22

Page 2: Ruby.new @ VilniusRB

@vidmantastwitter/github

Page 3: Ruby.new @ VilniusRB
Page 4: Ruby.new @ VilniusRB

Other companies (LT)

Page 5: Ruby.new @ VilniusRB

Worldwide companies

Page 6: Ruby.new @ VilniusRB

1995

Page 7: Ruby.new @ VilniusRB

2004

Page 8: Ruby.new @ VilniusRB

2.1.5

Page 9: Ruby.new @ VilniusRB

2.1.x

Page 10: Ruby.new @ VilniusRB

2.x.x

Page 11: Ruby.new @ VilniusRB

x.x.x

Page 12: Ruby.new @ VilniusRB

Why?

Ruby is designed for expressiveness & happiness

Open source, from the heart: licensing, friction, ecosystem1

1 - http://blog.codinghorror.com/why-ruby/

Page 13: Ruby.new @ VilniusRB

One of the many

C Java

Objective-CC++

C#PHP

Python

Javascript

Perl

VBR

SQL

RubyDelphi

F#

Page 14: Ruby.new @ VilniusRB

One of the many interpreted

C Java

Objective-CC++

C#PHP

Python

Javascript

Perl

VBR

SQL

RubyDelphi

F#

Page 15: Ruby.new @ VilniusRB

Ruby

Interpreted

Dynamic

Object-oriented

General purpose

Page 16: Ruby.new @ VilniusRB

Types

0 == "0"

Dynamically but strongly typed

Page 17: Ruby.new @ VilniusRB

Types

0 == "0".to_i

Dynamically but strongly typed

Page 18: Ruby.new @ VilniusRB

Vilniaus Ruby bendruomenė

describe "WiFi" do ssid "ruby" password "vilnius"end

Page 19: Ruby.new @ VilniusRB

More warmup examples

"vilniusrb".length

[1, 2, 3].include?(3)

"vilniusrb".include?("rb")

print "YOLO" if 1 > 2

2 * 2 + 2

Page 20: Ruby.new @ VilniusRB

Method definition

def hello puts "Hello Ruby"end

Page 21: Ruby.new @ VilniusRB

Calling the method

hello

Page 22: Ruby.new @ VilniusRB

Calling the method

hello

Hello Ruby

Page 23: Ruby.new @ VilniusRB

Calling the method

hello()

Hello Ruby

Page 24: Ruby.new @ VilniusRB

Method with parameter

def hello(name) puts "Hello #{name}"end

Page 25: Ruby.new @ VilniusRB

Calling the method & param

hello("PHP")

Hello PHP

Page 26: Ruby.new @ VilniusRB

Method with default param

def hello(name = "Ruby") puts "Hello #{name}"end

Page 27: Ruby.new @ VilniusRB

Calling the method & param

hello

Hello Ruby

Page 28: Ruby.new @ VilniusRB

Calling the method & param

hello ".NET"

Hello .NET

Page 29: Ruby.new @ VilniusRB

Flow control: if

if [1, "rb", nil].count >= 3 world_peace

else depression_with_chocolateend

Page 30: Ruby.new @ VilniusRB

The truth & the lies

false

nil

Page 31: Ruby.new @ VilniusRB

Check the truth

[]

0

Page 32: Ruby.new @ VilniusRB

Iteration

s = "Vilnius Ruby community".split

Page 33: Ruby.new @ VilniusRB

Iteration

s = ["Vilnius","Ruby", "community"

]

Page 34: Ruby.new @ VilniusRB

Iteration: the classics

for (i = 0; i < s.length; i++) {// do something with s[i]

}

Page 35: Ruby.new @ VilniusRB

Iteration: the Ruby way

s.each do |word| # do something with word puts word.upcaseend

Page 36: Ruby.new @ VilniusRB

Blocks

s.each do |word| # do something with word puts word.upcaseend

Page 37: Ruby.new @ VilniusRB

Blocks: implementation

def eachsize.times do |i|

yield(self[i])end

end

Page 38: Ruby.new @ VilniusRB

Blocks: implementation

def each size.times { |i| yield(self[i]) }end

Page 39: Ruby.new @ VilniusRB

Blocks: implementation

def each(&block)size.times do |i|

block.call(self[i])end

end

Page 40: Ruby.new @ VilniusRB

Enumerable

.map .max

.inject .min

.reject .partition

.all? .select

.any? .sort

.detect .first

Page 41: Ruby.new @ VilniusRB

Enumerable in action

[10, 5, 7] .map { |n| n * 2 } .select { |n| n > 10 } .sort

.first

Page 42: Ruby.new @ VilniusRB

Enumerable in action

[10, 5, 7] .map { |n| n * 2 } # [20, 10, 14]

.select { |n| n > 10 } # [20, 14]

.sort # [14, 20]

.first # 14

Page 43: Ruby.new @ VilniusRB

Objects

Everything is an object

Page 44: Ruby.new @ VilniusRB

Really

*Everything*

Page 45: Ruby.new @ VilniusRB

Classes are instructions

Page 46: Ruby.new @ VilniusRB

Objects - real instances

Page 47: Ruby.new @ VilniusRB

Classes are instructions

class Phone def initialize(name)

@name = name end

def bends? @name == :iphone endend

Page 48: Ruby.new @ VilniusRB

Objects - real instances

iphone = Phone.new(:iphone)iphone.bends? # true

nokia = Phone.new(:3310)nokia.bends? # false

Page 49: Ruby.new @ VilniusRB

It’s about time for magic!

Page 50: Ruby.new @ VilniusRB

Classes are always open

class String def last_capitalize reverse.capitalize.reverse endend

Page 51: Ruby.new @ VilniusRB

Classes are always open

class String def last_capitalize reverse.capitalize.reverse endend

"vilniusrb".last_capitalize

"vilniusrB"

Page 52: Ruby.new @ VilniusRB

Define methods, everywhere

vrb = "vilniusrb"

def vrb.rb_capitalize gsub("rb", "RB")end

vrb.rb_capitalize

"vilniusRB"

Page 53: Ruby.new @ VilniusRB

Missing a method?

class Phone def method_missing(mname, *args, &block) puts "I don’t know about `#{mname}`" puts caller.first endend

Page 54: Ruby.new @ VilniusRB

Missing a method?

def method_missing(mname, *args, &block) puts "I don’t know about `#{mname}`" puts caller.firstend

Phone.new.flips?

I don’t know about `flips?`/my/file/path/phone.rb:42:in `<main>`

Page 55: Ruby.new @ VilniusRB

Who’s who?

class class Phone def initialize(&block) instance_eval(&block) end

def answer! puts "Howdy?" endend

Page 56: Ruby.new @ VilniusRB

Who’s who?

def initialize(&block) instance_eval(&block)enddef answer! puts "Howdy?"end

Phone.new { answer! }

Page 57: Ruby.new @ VilniusRB

The Metaprogramming

.instance_eval

.class_eval

.define_method

.send

.method_missing

.instance_variable_set / get

Page 58: Ruby.new @ VilniusRB
Page 59: Ruby.new @ VilniusRB

Ruby

Interpreted

Dynamic

Object-oriented

General purpose

Page 60: Ruby.new @ VilniusRB

Ruby

Interpreted

Dynamic

Object-oriented

General purposeFUN

Page 62: Ruby.new @ VilniusRB

Time for fun!

house = House.new(2, 4, 5)house.floors # 2house.volume # Volume is 120 cubic meters.house.needs_elevator? # Yes

Page 63: Ruby.new @ VilniusRB

Time for fun! And magic

describe "WiFi" do ssid "ruby" password "vilnius"end

# { "WiFi": { "ssid": "ruby", "password": "vilnius" } }

Page 65: Ruby.new @ VilniusRB

Open source & gems

gem install credit_card_validator

require "credit_card_validator"number = '1111 2222 3333 4444'CreditCardValidator::Validator.valid?(number) # false

Page 66: Ruby.new @ VilniusRB

RubyA programmer’s best friend

Page 67: Ruby.new @ VilniusRB

Vilniaus Ruby bendruomenė

Questions.any?