Baby steps to a life on rails

Who is this for?

1: i have been learning ruby for a Month or two and want to get starred with rails

2: i just started learning ruby on rails However find myself confused by its syntax

if you find Yourself in any of these categories this blog might be very helpful for you

to start with We will take a look at the Hash along with symbols

Quick hash facts

  • the start and end of a hash is indicated by a set of {}
  • a hash can be Thought of as an array with out an index
  • A hash key can be nearly any object

The previous example uses a string as a hash key however rails uses symbols

:Symbols vs “strings”

  • a symbol is not part of the string class, Consequently string methods will not work on it.
  • Symbols are immutable meaning once they are created they cannot be modified, Which explains Why they have a very small number of functions associated with it.

The example above shows how symbols do not change and are great for naming attributes which you do not want them to change

Ruby evolves

sine it was that common for hashes to use symbols as keys as of ruby 1.9 there was a new syntax introduced for this case.

The new syntax replaces symbol For a colon after the key And removes the need for the Hash rocket symbol

Important! :name is valid ruby syntax while name: is only valid in the Context of a hash key

final words

Rails is not a fan of using () or {} when not needed

for example this code take from the application.html.erb file in a rails aplication

it is missing Both parentheses and curly braces. And the reason being in ruby the () are not needed by a function call as well as brackets are not needed when a hash is the last argument in a function call.

Software engineer