Ruby is...

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.

# The Greeter class
class Greeter
  def initialize(name)
    @name = name.capitalize
  end

  def salute
    puts "Hello #{@name}!"
  end
end

# Create a new object
g = Greeter.new("world")

# Output "Hello World!"
g.salute

Alt er objekter

5.times { print "We *love* Ruby -- it's outrageous!" }
class Numeric
  def plus(x)
    self.+(x)
  end
end

y = 5.plus 6
# y is now equal to 11
class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?
      can :manage, :all
    else
      can :read, :all
    end
  end
end
class Ability
  include CanCan::Ability

  def initialize(user)
    if user.admin?()
      can(:manage, :all)
    else
      can(:read, :all)
    end
  end
end

() yes, no, maybe

class SomeClass
  def initialize(value1)
    @value1 = value1
  end
 
  def value_printer(value2)
    lambda {puts "Value1: #{@value1}, Value2: #{value2}"}
  end
end
 
def caller(some_closure)
  some_closure.call
end
 
some_class = SomeClass.new(5)
printer = some_class.value_printer("some value")
 
caller(printer)

We got closure

Standardverdier som parameter

def sum(a = 1, b = 2, c = 3)
    a + b + c
end

sum 3, 3
=> 9

sum
=> 6

def tables(t = {})
    puts t
end

tables(absolute: true, round: true, precision: 2)
=> "{:absolute=>true, :round=>true, :precision=>2}"

Kun "false" og "nil" er false

x = true
y = x || 2
=> # y == 2

x = nil || y
=> # x == 2

# alt er som nevnt objekter
def nil.quack
    puts "quack quack" 
end
x = nil

x.quack
=> quack quack

Multippel returverdi

def foo(x)
 return x, x+1
end

a, b = foo(10)
#=> [10, 11]
class Publication
  attr_accessor :publisher
end

class Magazine < Publication
  attr_accessor :editor
end

m = Magazine.new
m.publisher = "Time Inc."

m.is_a? Magazine #=> true
m.is_a? Publication #=> true
m.class == Publication #=> false

Single inheritance

When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck

# p036duck.rb
class Duck
  def quack
    'Quack!'
  end
  
  def swim
    'Paddle paddle paddle...'
  end
end

class Goose
  def honk
    'Honk!'
  end
  def swim
    'Splash splash splash...'
  end
end

class DuckRecording
  def quack
    play
  end
  
  def play
    'Quack!'
  end
end
require_relative 'p036duck.rb'

def make_it_quack(duck)
  duck.quack
end

puts make_it_quack(Duck.new)
# Quack!
puts make_it_quack(DuckRecording.new)
# Quack!

def make_it_swim(duck)
  duck.swim
end

puts make_it_swim(Duck.new)
# Paddle paddle paddle...
puts make_it_swim(Goose.new)
# Splash splash splash...

How do I do that?

Ting man bør vite

Løkker

a = [ "a", "b", "c" ]
a.each {|x| print x, " -- " }
# a -- b -- c --

a = [ "a", "b", "c" ]
a.each_index {|x| print x, " -- " }
# 0 -- 1 -- 2 --

h = { "a" => 100, "b" => 200 }
h.each {|key, value| puts "#{key} is #{value}" }
# a is 100
# b is 200

Funksjoner

def someMethod(value)
  puts "#{Time.now}: #{value}"
end

someMethod('log this with timestamp')
# 2017-05-30 22:17:19 +0200: log this with timestamp

Import av filer

load 'somefile.rb'
# Laster filen på nytt hver gang den blir kalt

require 'sinatra'
# Laster inn bibliotek/fil en gang

require_relative 'libs/my_helper.rb'
# Last inn en fil relativt til hvor filen som kaller den står

Variabler

# Global
$x = 10
defined? $x
# "global-variable"

# Lokale variabler
loopcounter = 10
_LoopCounter = 20

defined? loopcounter
# "local-variable"

# Constant (kinda)
Number = 123
NUMBER = 321
defined? NUMBER
# "constant"
# Class
@@total = 0
defined? @@total
# "class variable"

# Instance
@total = 10
defined? @total
# "instance-variable"

If/else

x = 1
if x > 2
   puts "x is greater than 2"
elsif x <= 2 and x!=0
   puts "x is 1"
else
   puts "I can't guess the number"
end

$debug = 1
print "debug\n" if $debug

x = 1 
unless x>2
   puts "x is less than 2"
 else
   puts "x is greater than 2"
end

$var =  1
print "1 -- Value is set\n" if $var
print "2 -- Value is set\n" unless $var

$var = false
print "3 -- Value is set\n" unless $var
$age =  5
case $age
when 0 .. 2
   puts "baby"
when 3 .. 6
   puts "little child"
when 7 .. 12
   puts "child"
when 13 .. 18
   puts "youth"
else
   puts "adult"
end

Sweet regex

IRB = Interactive Ruby Shell

% irb
irb(main):001:0> 1 + 1
2
irb(main):002:0> def t(x)
irb(main):003:1> x+1
irb(main):004:1> end
=> nil
irb(main):005:0> t(3)
=> 4
irb(main):006:0> if t(3) == 4
irb(main):007:1> p :ok
irb(main):008:1> end
:ok
=> :ok
irb(main):009:0> quit
%

#!/usr/bin/ruby

Gem

source 'https://rubygems.org'

ruby '2.4.1'

gem 'rails', '~> 5.0'

gem 'pg', '~> 0.20'

# Use Puma as the app server
gem 'puma', '~> 3.8'

gem 'haml', '~> 4.0'
gem 'redcarpet', '~> 3.4'
gem 'uglifier', '~> 3.1'
gem 'therubyracer', '~> 0.12'
gem 'pikaday-gem', '~> 1.4'
gem 'jquery-rails', '~> 4.3'
gem 'turbolinks', '~> 2.5'

group :production do
  # To enable features such as static asset serving and logging on Heroku
  gem 'rails_12factor', '~> 0.0'
end

# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', '~> 9.0'
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 3.3'
  # Spring speeds up development by keeping your application running in the background.
  # Read more: https://github.com/rails/spring
  gem 'spring', '~> 2.0'
end

JRuby

# This is the 'magical Java require line'.
require 'java'

# With the 'require' above, we can now refer to things that are part of the
# standard Java platform via their full paths.
frame = javax.swing.JFrame.new("Window") # Creating a Java JFrame
label = javax.swing.JLabel.new("Hello")

# We can transparently call Java methods on Java objects, just as if 
# they were defined in Ruby.
frame.add(label)  # Invoking the Java method 'add'.
frame.setDefaultCloseOperation(javax.swing.JFrame::EXIT_ON_CLOSE)
frame.pack
frame.setVisible(true)

Ruby på Windows

Spørsmål?

Ruby is...

By Kyrre Havik

Ruby is...

  • 200