Customizing the items from a select field using Set operators

I had to customize the items of a select input field for timezones in a form. I needed to show some preferred US timezones first and then list the remaining below them. My mentor sent me this link to help me and I want to share it with you.

It turns out, this is something that you probably saw in a Math class: Set theory. What we need here is what we call Union:

A union is essentially used to combine the unique values of two sets, or in this case, arrays. To perform a union on two arrays you use the pipe as an operator. For example:

[1, 2, 1, 2, 3] | [1, 2, 3, 4] #=> [1, 2, 3, 4]

Mike Farmer

Or visually:

image from mathscrunch

Now that we are a little more familiar with set operators, let’s move to our problem. To keep our code organized, let’s move our code to a helper:

 # we use helpers to extract logic from our views
  
def timezones
  priorities =
    [ ActiveSupport::TimeZone['Eastern Time (US & Canada)'],
     ActiveSupport::TimeZone['Central Time (US & Canada)'],
     ActiveSupport::TimeZone['Mountain Time (US & Canada)'],
     ActiveSupport::TimeZone['Pacific Time (US & Canada)'] ]
    
  priorities | ActiveSupport::TimeZone.us_zones
end

So first we have a list of the preferred timezones, then we use the | operator to show those first and after that, show all the US time zones that aren’t included on the priorities list.

Now we just need to call our helper method on our form:

# html.slim

= f.collection_select :time_zone, timezones, :name, :to_s, {}, { class: "form-control" }

And voilà, that’s the result:

ta-da!

Whenever you have a similar problem, you can follow this approach. Hope you liked it!

Creating your own Ruby new method

I’ve been studying more of Ruby from RubyTapas and I even got back to an old project of mine, the RubyFarm. I’m here today to show one really cool thing that I learned today: how to personalize the new method.

In order to do that, Ruby needs from us three things (from RubyTapas):

1 – Allocate a new, empty object of the given class.
2 – Run any specialized initialization code defined for the class
3 – Return the initialized instance

So if we would rewrite the RubyFarm Animal new instance method, for example, we could do something like:

class Animal
  def self.create_new(*args, &block)
    instance = allocate
    instance.my_initialize(*args, &block)
    instance
  end

  def my_initialize(breed, age, weight, last_vaccine)
    @breed = breed
    @age = age
    @weight = weight
    @last_vaccine = last_vaccine
  end
end

bob = Animal.create_new('domestic cat', 5, 8, 2017)
p bob # => <Animal:0x00005644aaa17800 @breed="domestic cat", @age=5, @weight=8, @last_vaccine=2017>

And voilà! Ruby really makes it easier to creates your own methods. Thanks, Avdi for all of those lessons 🙂

Hope you found that as awesome as I did when I learned this. See you next time!

Object-Oriented Programming with Ruby pt. 1

I had OOP in my college last year and it was a bummer. Looking back, I may have learned the most valuable skill for developers: being a developer == a full-time self-learner (a new concept that I just invented).

But since I quit college and started my internship last year, I felt the need to go back to the basics and finally be able to write good code using OOP fundamentals. I started a Rails tutorial and I noticed that I have never run a Ruby program (without rails) on my terminal!

What I learned most from this experience was that I should focus on learning the foundations before the frameworks, for example: learn Ruby before Ruby on Rails. I wanted to understand why Ruby became so popular and what makes OOP a paradigm so important in Computer Science.

I found a lot of resources online and one of them is the MIT Computer Science which offers a complete Introduction to Computer Science and Programming with Python and I decided to make some notes about it but in Ruby. OOP is given in lectures 14, 15 and 16 if you want to give it a try too. Also found the Docs — Programming Ruby which is a complete material to follow. Both these materials helped me study and are guiding this post 🙂

This is the part one, which will be an introduction to OOP introductory concepts, like Class and Objects. I also started a repository on GitHub where I apply what I am studying to keep a record.

Abstract everything

OOP is not a new concept in Computer Science, but it became very popular only on the 70’s. But why do we need OOP and why did it become so popular? Quoting Prof. Eric Grimson:

Object Oriented Programming is great when you are trying to model systems that consist of a large number of units that interact in various ways.

That’s why OOP is so vastly used and solves a huge amount of problems in our real life because we can solve almost every problem with this paradigm! By gathering together some data it allows you to write better code, which means it is easier to test and to refactor.

we are all objects

Let’s go back to the basics

Everyone who starts to study OOP start to hear a lot about ClassesObjects, and Instances. Let’s explore them a little theoretically.

Classes, Objects, Instances

You must have heard that in Ruby, everything is an object: a Class is an Object; An Instance is an Object and an Object is… I think you get the idea! Although in our real world we deal with lots of objects, it’s hard to abstract it to code — at least for me. But once we understand the purpose of it and the concepts behind it, it starts to make more sense and things start to get easier #hopefully.

A Class is a collection of data and functions that make sense to stay together because they have characteristics in common. It helps your daily work by preventing you from spreading pieces of code through different files.

As prof. Eric Grimson said in one of his classes, it’s easier if think of a Class like a stamp: you can print several objects of a Class using its stamp. Every new object created using a stamp will have the same characteristics but they will still be different objects because they are unique copies of that stamp, they’re randomly allocated in different locations in memory.

And why are objects so useful? Because we can manipulate them as we need! What would be of the human race without manipulating everything around, right?!

But how does that Class definition is applied to Objects? Well, when you create a new object it comes with some data. You are instantiating a new Object from a specific Class. You can call this object’s data as attributes. You can define them in your initialize method (a special Class method), for example.

You can really manipulate them to your needs with the functions associated with its Class. You can call them instance methods. Now let’s create a simple Class to see this in action.

So instance and objects are pretty much the same, it’s up to you how you wanna call it 🙂

Show me the code

We are going to create animal instances from the Animal Class (you can also see the code on GitHub). But before diving into the code, let’s think about the implementation a little. Let’s say we need to create an Animal Class so it’s easier to create a lot of cute animals to play with.

What does an animal have? What does it do? (We don’t need to think about everything now, we can always add/remove any information about our classes whenever we want).

We know that animals have a name, an age, a weight and a date of vaccination. These are the attributes. We also know that animals need annual vaccination, sleeping and playing. These are the object/instance methods. We’ll write a method to check if the animal has taken its vaccines, so if a new animal with outdated vaccination is created, we’ll get an alert.

We can put this into a file called animal.rb:

class Animal
  # The initialize method is a special Class method.
  def initialize(name:, age:, weight:, last_vaccine_year:)
    @name = name
    @age = age
    @weight = weight
    @last_vaccine_year = last_vaccine_year
  end

  def is_vaccinated?
     current_year = Time.now.year
     return true if current_year - last_vaccine_year <= 1
     "#{name} has not been vaccinated this year yet"
  end
end

What’s next

This simple Animal Class works fine, but it’s pretty generic, don’t you think?. What if I want to have cats, dogs, birds, fishes, horses…? I am going to need to think of Classes like Species in Biology… cool, right? But that will be our next subject when we will discuss Inheritance and Encapsulation.

If you have any questions or want to suggest/contribute feel free to comment here. I am learning Ruby as well, so any suggestion is welcome. See you soon!