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

:yesemployee = {:name => "Abhinav", :age => 22}class Employee
attr_accessor :name
end
"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(' ')
end
end
k = Greeting.new
k.send :hello, "gentle", "readers" # "Hello gentle readers"
#----------------------------------------------------------class Dog
attr_accessor :name
end

dog1 = Dog.new
dog1.name = "Einstein"

What is @ and @@ in class?

class Test
@@shared = 1

def value
@@shared
end

def value=(value)
@@shared = value
end
end

class AnotherTest < Test; end

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

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

a = AnotherTest.new
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?

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

render :signup   #or render "signup"
redirect_to signup_path

Resource CRUD — using :only and :except

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

Instance variables vs local variables

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

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?

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

What is rbenv?

What is gem? And where is it stored?

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

What is Bundler in rails?

  • 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

--

--

--

Buliding influencerbit.com | Guitarist | Traveller | Entrepreneur

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Geographically distributing workloads in Python using Celery, RabbitMQ, and Redis

Bash Scripting Tutorial — part 9

Why my Rails Projects Couldn’t Connect to Postgresql

𝗧𝗿𝗮𝗱𝗲𝗿, defier, NFT. Everything in Web 3.0

Review: Curiosity HPC development board

What and why Laravel? Is Laravel difficult to digest?

The importance of time to think in Software Development

DEVELOPMENT

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 influencerbit.com | Guitarist | Traveller | Entrepreneur

More from Medium

Let’s Start Rebuilding rails

Rails ActiveRecord Callbacks

Phase-4 | Ruby on Rails!

Save Data Automatically with Ruby on Rails