Vilniaus Ruby bendruomenė Ruby.new Vidmantas Kabošis @ VilniusRB.new 2014-11-22
Vilniaus Ruby bendruomenė
Ruby.newVidmantas Kabošis @ VilniusRB.new
2014-11-22
@vidmantastwitter/github
Other companies (LT)
Worldwide companies
1995
2004
2.1.5
2.1.x
2.x.x
x.x.x
Why?
Ruby is designed for expressiveness & happiness
Open source, from the heart: licensing, friction, ecosystem1
1 - http://blog.codinghorror.com/why-ruby/
One of the many
C Java
Objective-CC++
C#PHP
Python
Javascript
Perl
VBR
SQL
RubyDelphi
F#
One of the many interpreted
C Java
Objective-CC++
C#PHP
Python
Javascript
Perl
VBR
SQL
RubyDelphi
F#
Ruby
Interpreted
Dynamic
Object-oriented
General purpose
Types
0 == "0"
Dynamically but strongly typed
Types
0 == "0".to_i
Dynamically but strongly typed
Vilniaus Ruby bendruomenė
describe "WiFi" do ssid "ruby" password "vilnius"end
More warmup examples
"vilniusrb".length
[1, 2, 3].include?(3)
"vilniusrb".include?("rb")
print "YOLO" if 1 > 2
2 * 2 + 2
Method definition
def hello puts "Hello Ruby"end
Calling the method
hello
Calling the method
hello
Hello Ruby
Calling the method
hello()
Hello Ruby
Method with parameter
def hello(name) puts "Hello #{name}"end
Calling the method & param
hello("PHP")
Hello PHP
Method with default param
def hello(name = "Ruby") puts "Hello #{name}"end
Calling the method & param
hello
Hello Ruby
Calling the method & param
hello ".NET"
Hello .NET
Flow control: if
if [1, "rb", nil].count >= 3 world_peace
else depression_with_chocolateend
The truth & the lies
false
nil
Check the truth
[]
0
Iteration
s = "Vilnius Ruby community".split
Iteration
s = ["Vilnius","Ruby", "community"
]
Iteration: the classics
for (i = 0; i < s.length; i++) {// do something with s[i]
}
Iteration: the Ruby way
s.each do |word| # do something with word puts word.upcaseend
Blocks
s.each do |word| # do something with word puts word.upcaseend
Blocks: implementation
def eachsize.times do |i|
yield(self[i])end
end
Blocks: implementation
def each size.times { |i| yield(self[i]) }end
Blocks: implementation
def each(&block)size.times do |i|
block.call(self[i])end
end
Enumerable
.map .max
.inject .min
.reject .partition
.all? .select
.any? .sort
.detect .first
Enumerable in action
[10, 5, 7] .map { |n| n * 2 } .select { |n| n > 10 } .sort
.first
Enumerable in action
[10, 5, 7] .map { |n| n * 2 } # [20, 10, 14]
.select { |n| n > 10 } # [20, 14]
.sort # [14, 20]
.first # 14
Objects
Everything is an object
Really
*Everything*
Classes are instructions
Objects - real instances
Classes are instructions
class Phone def initialize(name)
@name = name end
def bends? @name == :iphone endend
Objects - real instances
iphone = Phone.new(:iphone)iphone.bends? # true
nokia = Phone.new(:3310)nokia.bends? # false
It’s about time for magic!
Classes are always open
class String def last_capitalize reverse.capitalize.reverse endend
Classes are always open
class String def last_capitalize reverse.capitalize.reverse endend
"vilniusrb".last_capitalize
"vilniusrB"
Define methods, everywhere
vrb = "vilniusrb"
def vrb.rb_capitalize gsub("rb", "RB")end
vrb.rb_capitalize
"vilniusRB"
Missing a method?
class Phone def method_missing(mname, *args, &block) puts "I don’t know about `#{mname}`" puts caller.first endend
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>`
Who’s who?
class class Phone def initialize(&block) instance_eval(&block) end
def answer! puts "Howdy?" endend
Who’s who?
def initialize(&block) instance_eval(&block)enddef answer! puts "Howdy?"end
Phone.new { answer! }
The Metaprogramming
.instance_eval
.class_eval
.define_method
.send
.method_missing
.instance_variable_set / get
Ruby
Interpreted
Dynamic
Object-oriented
General purpose
Ruby
Interpreted
Dynamic
Object-oriented
General purposeFUN
And profit!
Time for fun!
house = House.new(2, 4, 5)house.floors # 2house.volume # Volume is 120 cubic meters.house.needs_elevator? # Yes
Time for fun! And magic
describe "WiFi" do ssid "ruby" password "vilnius"end
# { "WiFi": { "ssid": "ruby", "password": "vilnius" } }
“Answers”
bit.ly/vilniusrb-1
bit.ly/vilniusrb-2
Open source & gems
gem install credit_card_validator
require "credit_card_validator"number = '1111 2222 3333 4444'CreditCardValidator::Validator.valid?(number) # false
RubyA programmer’s best friend
Vilniaus Ruby bendruomenė
Questions.any?