Ruby Training

From bibbleWiki
Jump to navigation Jump to search

Introduction

This is to summarised what I learn on the ruby Training

Returning if true

Quite liked this. On do the return if true

  def remaining_minutes_in_oven(actual_minutes_in_oven)
    return EXPECTED_MINUTES_IN_OVEN - actual_minutes_in_oven if actual_minutes_in_oven < EXPECTED_MINUTES_IN_OVEN
    raise 'Please implement the Lasagna#remaining_minutes_in_oven method'
  end

Symbols

Symbols are just an identifier

class User
  STATES = [:active, :inactive, :banned]
...

Provide you pass the same thing, they are the same thing e.g.

User.new(:active)


Predicate Method

So in ruby you have a predicate method of a method that returns true or false which is, by convention, not enforced, a method with a ?. The example below returns true if status equal true.

class User
  STATES = [:active, :inactive, :banned]

  def initialize(status)
    raise "Invalid state" unless STATES.include?(status)
    @status = status
  end

  def active?
    @status == :active
  end
end

This became a bit more obvious the usage with the example with ranges

module Chess
  RANKS = 1..8
  FILES = 'A'..'H'

  def self.valid_square?(rank, file)
    RANKS.include?(rank) && FILES.include?(file)
  end
end

Interpolation

This is how to interpolation works

my_var1 = "HELLO"
my_var2 = "FRED WAS HERE #{my_var1}"
puts my_var2

LINQ in Ruby?

So this is what was given

class BirdCount
  def self.last_week
    [0, 2, 5, 3, 7, 8, 4]
  end

  def initialize(birds_per_day)
    @birds_per_day = birds_per_day
  end

  def yesterday
    @birds_per_day[-2]
  end

  def total
    @birds_per_day.sum
  end

  def busy_days
    @birds_per_day.count { |day| day >= 5 }
  end

  def day_without_birds?
    @birds_per_day.any? { |day| day == 0 }
  end
end

Which looks canna complicated but really it just like this in C#

public class BirdCount
{
    public int[] BirdsPerDay { get; }

    public BirdCount(int[] birdsPerDay)
    {
        BirdsPerDay = birdsPerDay;
    }
}

var birds = new BirdCount(new[] { 0, 2, 5, 3, 7, 8, 4 });
birds.Where(day => day >= 5).Count();


Other functions include

fibonacci = [0, 1, 1, 2, 3, 5, 8, 13]

fibonacci.count  { |number| number == 1 }   #=> 2
fibonacci.any?   { |number| number == 6 }   #=> false
fibonacci.select { |number| number.odd? }   #=> [1, 1, 3, 5, 13]
fibonacci.all?   { |number| number < 20 }   #=> true
fibonacci.map    { |number| number * 2  }   #=> [0, 2, 2, 4, 6, 10, 16, 26]

Mapping over arrays

This was quite hard for me. I think just not used to syntax. Once I see the answer it is obvious. compact removes the nil values in an array.

# So an example of the data is

    shoes = { price: 30.00, name: "Shoes", quantity_by_size: { s: 1, xl: 4 } }
    coat = { price: 65.00, name: "Coat", quantity_by_size: { s: 2 } }
    handkerchief = { price: 19.99, name: "Handkerchief", quantity_by_size: { m: 3, l: 2 } }
    items = [shoes, coat, handkerchief]

class BoutiqueInventory
  def initialize(items)
    @items = items
  end

  def item_names
    @items.map { |item| item[:name] }.flatten.sort
  end

  def cheap
    @items.map { |item| item[:price] < 30.00 ? item : nil }.compact
  end

  def out_of_stock
    @items.map { |item| item[:quantity_by_size].empty? ? item : nil }.compact
  end

  def stock_for_item(name)
    @items.map { |item| item[:name] == name ? item[:quantity_by_size] : nil }.compact.first || {}
  end

  def total_stock
    @items.map { |item| item[:quantity_by_size].values.sum }.sum
  end

  private
  attr_reader :items
end