Dirty Ruby Rundown

As said in the title, this is a quick and dirty summarization of Ruby and what you can do with it! By no means is this an in depth look into the language, but rather summary with broad strokes to refresh people who have used Ruby before but haven’t done so in a while. That’s exactly the motivation behind this post! I, myself, haven’t coded in Ruby on a daily basis in a few months, so when I had to return to Ruby, I wanted to quickly refresh myself.

First, install Ruby!

Now that you have Ruby, you can access IRB (Interactive Ruby) where you can code in real time and play with the language.

puts "Hello World"
// Hello World
// => nil

// Puts is the basic command to print something from Ruby, similar to console.log() in JavaScript. The Nil value is always returned from 'puts'.

Basic operators!

3+3 
// => 6

3*3
// => 9

3**3
// => 27

Math.sqrt(64)
// => 8.0

Huh, what Math? Math, in this case, is a built in module. Think of a module as a library hosting many functions and methods that can be used in various instances so that you don’t have to recode the same functions in multiple parts of your code or classes. We’ll talk about this a bit more after we talk about classes.

Methods

def Hello
  puts "Hello World!"
end

Hello
// => "Hello World!"
// =>  nil

Methods have the “def NAME end” syntax, in contrast to Javascript’s “function NAME( ){ }” or “NAME = ( ) => { }”. There are a lot of extra neat things to explore such as implicit returns! In short, Ruby has a lot of neat little features that are designed to make the development life enjoyable, implicit returns, among others, is an example of such a feature.

Classes

class Person
  def initialize(name, age, favorite_color)
    @name = name
    @age = age
    @favorite_color = favorite_color
  end

  def introduction
    puts "Hi! My name is #{@name} and I am #{@age} years old. My favorite color is #{@favorite_color}. How about yours?"
  end

  def goodbye
    puts "Bye! It was great chatting!"
  end
end

In short, a class is an object that can be defined by the characteristic structure set up within these classes. Let’s create one!

person = Person.create(name: 'Alejandro', age: '22', favorite_color: 'green')

And let’s have this person introduce himself!

person.introduction
// => Hi! My name is Alejandro and I am 22 years old. My favorite color is green. How about yours?
// => nil

There are a great many things you can do with classes and it helps reduce the code you have to write, especially when grouping certain types of objects together. There are also deeper things to explore such as attr_accessors and attr_readers which make using classes even easier!

Modules

Modules can be used outside of classes as we saw earlier with ‘Math’, but they can also be used within classes!

module Human
  def human?
    true
  end
end

class Person
  include Human

  def initialize(name, age, favorite_color)
    @name = name
    @age = age
    @favorite_color = favorite_color
  end

  def introduction
    puts "Hi! My name is #{@name} and I am #{@age} years old. My favorite color is #{@favorite_color}. How about yours?"
  end

  def goodbye
    puts "Bye! It was great chatting!"
  end
end

Here we’re using the same class we created earlier but also creating and including a modules within this class.

person = Person.create(name: 'Alejandro', age: '22', favorite_color: 'green')

p person.human?
// => true

First, notice that ‘p’ also prints out data similar to ‘puts’ but there’s a key difference in value type being returned. Second, what happened here? ‘human?’ isn’t a Person class method, so how was it called? Well, exactly the same way we included the module into the Person class. In short, it’s as if we defined the ‘human?’ method in the Person class all along. This can be useful when you have other classes that may also need a ‘human?’ function, maybe a Professor class or a Teacher class?

In conclusion, Ruby is an amazing and enjoyable language to use. It’s a language built by software engineers for software engineers! It can be very powerful when wielded properly but perhaps the greatest power it gives lies in it’s consideration to the average software engineer because it proposes the fact that software engineering doesn’t have to be harder than it needs to be. Why not the opposite?

Leave a comment

Design a site like this with WordPress.com
Get started