Ruby basics

Twitter: @krzkot

Github: lis2

Email: kotlarek.krzysztof@gmail.com

  • Yukihiro Matsumoto
  • 1995
  • Based on Perl, Smalltalk, Eiffel, Ada, Lisp
  • Offers automatic memory management
  • Written in C
  • It is a strong object oriented programming language
  • Open Source
  • Simple
  • Easy to write
  • Elegant
  • David Heinemeier Hansson
  • December 2005

Comments


  # example comment
      

    puts("Hello World") 
      

    puts("Hello World") # Hello World
      

    puts "Hello World" # Hello World
      

Variables

Duck typing

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


  name = "John"
  age = 29
  a = [2, 3, 1, 4, 5]
  h = { name: 'John', age: 29 }
  
  puts name.upcase # JOHN
  puts age # 29
  puts a.sort # [1, 2, 3, 4, 5]
  puts a[:name] # John
      

  name = "John"
  age = 29
  a = [2, 3, 1, 4, 5]
  h = { name: 'John', age: 29 }
  
  puts name.upcase # JOHN
  puts age # 29
  puts a.sort # [1, 2, 3, 4, 5]
  puts a[:name] # John
  
  puts name.class # String
  puts age.class # Fixnum
  puts a.class # Array
  puts h.class # Hash
      

Everything is an object


  puts 1 + 2 # 3
      

   class Fixnum
     def +(other)
       return 4
     end
   end
   puts 1 + 2 # 4
      

Functions


  def full_name(first_name, last_name)
    return "#{first_name} #{last_name}"
  end
  
  puts full_name('John', 'Snow') # John Snow
      

  def full_name(first_name, last_name)
    "#{first_name} #{last_name}"
  end
  
  puts full_name('John', 'Snow') # John Snow
      

Yield


  def test
    puts "1"
    yield 2
    puts "3"
  end
  test() { |number| puts number }
  # 1
  # 2
  # 3
      

  def full_name(first_name, last_name)
    "#{first_name} #{last_name}"
  end
  
  def greeting(first_name, last_name)
    name = full_name(first_name, last_name)
    yield(name)
  end
  
  greeting('John', 'Snow') {|full_name| puts "Welcome, #{full_name}"}
  # Welcome, John Snow
      

  def connect
    connect.connect('localhost:3000')
    connect.auth('lis2', 'password')
    yield connect
    connect.end
  end

  connect { |c| c.get_users }
  connect { |c| c.delete_user(1) }
  
      

Loops


  (1..10).each do |number|
    if number % 2 == 0
      puts number 
    end
  end # 2, 4, 6, 8, 10
      

  (1..10).each do |number|
      puts number if number % 2 == 0
  end # 2, 4, 6, 8, 10
      

Classes


  class Person
    def initialize(first_name, last_name)
      @first_name = first_name
      @last_name = last_name
    end
  
    def full_name
      "#{@first_name} #{@last_name}"
    end
  end

  person = Person.new('John', 'Snow')
  puts person.full_name # John Snow
      

  class Person
    attr_accessor :first_name
  
    def initialize(first_name, last_name)
      @first_name = first_name
      @last_name = last_name
    end
  
    def full_name
      "#{@first_name} #{@last_name}"
    end
  end

  person = Person.new('John', 'Snow')
  puts person.full_name # John Snow
  person.first_name = 'Peter'
  puts person.full_name # Peter Snow
      

Inherintance


  class Dog < Person
    def full_name
      "I am a dog - #{super}"
    end
  end
  
  dog = Dog.new('Einstein', 'Brown')
  puts dog.full_name # I am a dog - Einstein Brown
      

Tests

person_test.rb


  require "minitest/autorun"
  require "./person.rb"
  
  class TestPerson < Minitest::Test
    def setup
      @person = Person.new('John', 'Snow')
    end
  
    def test_full_name
      assert_equal "John Snow", @person.full_name
    end

    def test_fail
      assert_equal "Johnny Cage", @person.full_name
    end
  end
      

  class TestPerson < Minitest::Test
    def setup
      @person = Person.new('John', 'Snow')
    end
  
    def test_full_name
      assert_equal "John Snow", @person.full_name
    end

    def test_first_name
      assert_equal "John", @person.first_name
    end
  end
      

Metaprogramming


  def a1
    "a1_whatever"
  end
  
  def a2
    "a2_whatever"
  end
  
  def a3
    "a3_whatever"
  end
  puts a3 # a3_whatever
      

  ['a1', 'a2', 'a3'].each do |method_name|
    define_method method_name do
      "#{method_name}_whatever"
    end
  end
  puts a3 # a3_whatever
      

Downsides

Anyway, I wanna learn

KoRUG?

http://railspoint.com/ruby

Thank you