DEV Community

Cover image for Keeping up with Ruby
Ridwan Abiola
Ridwan Abiola

Posted on

Keeping up with Ruby

Ha Ruby! some real gem there, believe me. My first impressions checking through the docs, when first starting out was: "This looks like C (now I know better, it doesn't)". Little do I know am going to be writing a post about it some few odd months after.

So what made me hooked enough to want to explore the language more? Well am not yet sure but i will tell you something along the lines of:

  • It's syntax
  • Visual Appearance (makes a case for clear code out of the box)
  • Flexibility
  • Modules
  • Exception Handling

Best parts for me so far

Hashes

This will be equivalent of "Objects Literals" in JavaScript. Key/value pairs, two way binding, linked(you get the picture), they differ from Arrays in the sense that whereas Arrays are like lists, they are like dictionaries.

Create 'em:

  example_hash = {
    "one": "okan",
    "two": "eeji",
    "three": "eeta"
  }

Use 'em

example_hash["one"] # => okan

Love 'em

Read more about Hashes from the docs

Modules

Modules is a way of classifying methods and constants, watch the word "classify", even though similar (well kind of) to classes yet modules cannot be instantiated (objects cannot be created out of it) and the keyword "new" does not work on it.

Create 'em

module Alifabeti
  CONST = "A"
end

Use 'em

module Alifabeti
  def oro?
    true
  end
end

class Ede
  include Alifabeti
end

ede_yooba = Ede.new
puts ede_yooba.oro? # => true

Love 'em

Read more about Modules from the docs

Symbols

Symbols represents code in your code (am confused as well) rather than string. It is a unique identifier for piece of data you need in your code. Even though it is like a string, it is not a string (i think an example will do a better job than am trying to do right now).

Create 'em

apamo = :faweli

Use 'em

  apamo.object_id # => 1928988
  apamo.object_id # => 1928988
  apamo.object_id # => 1928988   

Did you notice it returned the same "object_id" for the that same symbol, Ruby only uses one object to represent all those symbols. And perhaps that's one of the obvious difference between symbols and string, for each time you use the same string in your code, it will create a new object each time.

Love 'em

Read more about Symbols from the docs

So there you have it, well am still exploring the language and hope to keep this going as a serial kinda thing, thanks for reading.

Latest comments (0)