Introduction to Ruby and Introduction to Ruby on Rails basic concepts for beginners. The google presentation is even better in full screen https://docs.google.com/presentation/d/1EE0VuB_PkD2-8j5JNs6CUQHb4J9ToIgC7-IxYTojiS0/pub?start=false&loop=false&delayms=3000#slide=id.p
Transcript of Ruby on Rails for beginners
I am Vysakh. I am a web developer.
In Ruby
puts “Hello world”
Ruby is simply simple
Just like english
Variables
You have - basket A with 10 apples - basket B with 20 apples.
Find the sum and difference
basketa = 10 applesbasketb = 20 apples
sum = basketa + basketb=> 10 + 20 = 30
difference = basketb - basketa=> 20 - 10 = 10
basket_a = 10basket_b = 20
sum = basket_a + basket_bdifference = basket_b - basket_a
In Ruby
It is just like what you wrote in your kindergarden answer sheet
Creator of Ruby - Matz
Running the code
Ruby is executed line by line
So output of a single line/logic can be seen using irb
Opening up irb in the terminal
Instead of executing as a program file. Useful for learning, debugging.
Conditionals
cold_weather = true
if cold_weather puts “Wear something warm”else puts “Don’t wear something”end
Ruby code
Most thing in Ruby is Object
2.odd?()=> false
“Vysakh”.length()=> 6
Brackets are optional in Ruby
2.odd?=> false
“Vysakh”.length=> 6
Exercise 1
a) Get a string and check if it is palindrome.
b) Get a number and print the reverse of it.
Hint: use gets to get the input.
Array/List
In Ruby
shopping = [ “milk”, “cheese”, “olive oil”]
Loops
In Ruby
20.times do puts “I must do my homework”end
Also, there are
- loop- while loop- for loop- step - upto
HAPPYRuby is all about making programmer
Mam, please take each item for me to bill
cart = [“biscuit”, milk”, “shampoo”, “tampons” ]
Mam, please take each item for me to bill
cart = [“biscuit”, milk”, “shampoo”, “tampons” ]
cart.each do |item| puts itemend
Exercise 2 - Create an array of marks you got in your last assessment. - Find if the sum of odd numbers is greater than the sum of even numbers, if it is, you have big odds at passing.- Pass this information or bad luck will follow
HASH
In Rubyslang = { “lol” => “laugh out loud”, “brb” => “Be right back” }
slang[“lol”] => laught out loud
Use symbols instead of stringslang = { :lol => “laugh out loud”, :brb => “Be right back” }
slang[:lol] => laught out loud
Symbols in Ruby
:hand.class=> Symbol
Alternate way to write hashslang = { lol: “laugh out loud”, brb: “Be right back” }
slang[:lol] => laught out loud
Exercise 3
- Create a list of ipl team hashes - with batsmen (3) name as key and their sixes as values.
a) Find the sixes hit by a batsmen of a given name
Functions
In Ruby
def y(x) x**3end
y(3) # => 27y(2) # => 8
Exercise 4
a) Create a method rajini_style. Return his dialogue (your favorite).
b) Create a factorial method that gets a number and returns the factorial
Class
class Sports def initialize(players, name) @no_of_players = players @name = name endendcricket = Sports.new(11, “Cricket”)foot_ball = Sports.new(11, “Football”)
Inheritance
a version manager (RVM or rbenv or other)
Install ruby using
rbenvInstalling Ruby using
Installing and setting Ruby
dhhCreator of Ruby on Rails
Installing Rails
Creating a new rails project
Files generated with “rails new myproject” command
Starting Rails server
Check the browserlocalhost:3000
Understanding Rails
A dress shop
Pantsection?
1st floor kid. 2nd section
1st floorhere is your pant
A Rails server
/about
routes.rb
home controller, about action
A Rails server
home controller, about action
here is your html file
Lets do it in Rails
filename: config/routes.rb
Configure the routes to get /about
The corresponding html
and go to ‘/about’ url
Lets open the browser
available in the section?
When the pant is not
Coming back to the kid and pant story
1st floor
I want this specific design
Godown
Oh kid lemme go to the godown and get
In RailsDatabase
home controller, about action
/posts/1
and have an abstraction(model)
Lets create a table
This will create model, migration files
migration - A ruby file that will create, modify tables
posts table
title column of type string
from the ruby file(migration)
Lets create the tables
Rake, a simple ruby build program with capabilities similar to make
This creates the posts table
table records through Rails model
Lets create, manipulate
Post is a Ruby class that Inherits ActiveRecord
app/models/post.rb
Object Relation Mapping(ORM) layer
Active Record is an
tables map to classes,
rows map to objectsrows map to objectsRows map to objects
Columns map to object attributes
Few operations
Lets use this query inRails controller and give it to the user
Configure the routes file
Open up the browser
Show the title, description in html
app/views/show.html.erb
Now lets check the browser
Now lets do all this in one command -> scaffold
This will generate all filesbased on REST Architecture