Ruby & Rails — some thirst satisfiers!

  • Symbols
  • Instance and Class variables
  • ERB
  • use of as: in rails route
  • Render Vs Redirect in Rails
  • Resources CRUD — using :only
  • Instance Variable vs Local Variable
  • Sending a message by redirect_to
  • Partials
  • Rbenv
  • Gem
  • Bundler
  • ! ’ in ruby


Symbols are the pills which we generally swallow without knowing what exactly are they! So what exactly are these things and why to use them?

:yesemployee = {:name => "Abhinav", :age => 22}class Employee
attr_accessor :name
"yes" + " Abhinav"       # "yes Abhinav"
:yes + "some string" # Error. Symbols are immutable!
"name".object_id    #70230235721980
"name".object_id #70230235705660
:name.object_id #86108
:name.object_id #86108
# not so efficient - Case 1
# ----------------
employeeDetails = {"name" : "Abhinav", "age" : 22}
# ----------------
# Efficient - Case 2
# -----------------
employeeDetails = {:name => "Abhinav", :age => 22}
# -----------------
person = {name: "Abhinav"}
Person.age(:dob => "1996")
class Greeting
def hello(*args)
"Hello " + args.join(' ')
k =
k.send :hello, "gentle", "readers" # "Hello gentle readers"
#----------------------------------------------------------class Dog
attr_accessor :name

dog1 = = "Einstein"

What is @ and @@ in class?

@ is an instance variable in class which is specific to an object of the class.

class Test
@@shared = 1

def value

def value=(value)
@@shared = value

class AnotherTest < Test; end

t =
puts "t.value is #{t.value}" # 1
t.value = 2
puts "t.value is #{t.value}" # 2

x =
puts "x.value is #{x.value}" # 2

a =
puts "a.value is #{a.value}" # 2
a.value = 3
puts "a.value is #{a.value}" # 3
puts "t.value is #{t.value}" # 3
puts "x.value is #{x.value}" # 3

What is <% %> or <%= %> in rails?

This is used in Embedded Ruby (ERB) template. Within an ERB template, Ruby code can be included using both <% %> and <%= %> tags. The <% %>tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks, and the <%= %> tags are used when you want output.

What is as: in routes.rb file in rails?

get 'signup', to: 'users#signup', as: 'signup1'
<%= link_to "Sign Up", signup1_path %>

Render Vs Redirect

Redirect is used to tell the browser to issue a new request. Whereas, render only works in case the controller is being set up properly with the variables that needs to be rendered. Render creates the response to be sent back to the browser. It doesn’t affect the url in the browser.

render :signup   #or render "signup"
redirect_to signup_path

Resource CRUD — using :only and :except

By default, Rails creates routes for the seven default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application by using resource. You can use the :only and :except options to fine-tune this behavior. The :only option tells Rails to create only the specified routes.

resources :mobiles, :only => [:index, :show]

Instance variables vs local variables

A local variable that is defined inside one method, for example, cannot be accessed by another method. In order to get around this limitation, we can use instance variables inside Ruby classes. Instance variables are the variables starting with @ sign.

How to send a message from redirect_to to the redirected page?

One can think of passing arguments through redirect and then catching them through params[:]. Thats what my initial thought was!

redirect_to controller: 'user', action: 'edit', id: 3, something: 'parameter'
# would yield /user/3/edit?something=parameter
flash[:notice] = "Post successfully created"# --------------------------------------------
# in show.html.erb
<% if flash[:notice] %>
<div class="notice"><%= flash[:notice] %></div>
<% end %>

What is Partials?

Partial templates — usually just called “partials” — are another device for breaking the rendering process into more manageable chunks. With a partial, we can move the code for rendering a particular piece of a response to its own file.

# Filename is _navbar.html.erb in layouts directory in views.
<%= render 'layouts/navbar' %>

What is rbenv?

Rbenv stands for Ruby Environment. We don’t have to use the system ruby everytime for the project. We can have different local versions of ruby to run different projects. More information here.

What is gem? And where is it stored?

Most of the time, gems just work. We require them and they do the work. A good read on how the gems work is this.

$ gem environmentOutput: /Users/<username>/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems

What is Bundler in rails?

Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed. Bundler is an exit from dependency hell, and ensures that the gems you need are present in development, staging, and production.

  • It installs all the gems we need
  • and it locks RubyGems down, so those gems are the only ones we can require inside that Rails app. Thats why Gemfile.lock is made after every bundle install.

! in Ruby

foo = "A STRING"  # a string called foo
foo.downcase! # modifies foo itself
puts foo # prints modified foo



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhinav Rai

Abhinav Rai

Buliding | Guitarist | Traveller | Entrepreneur