Learn coding with amol
Hello my name is amol sharma or vineet and this is my 11th part of learn coding with amol and in this part we are going to learn Ruby.
Ruby PART-11
Complete Ruby Programming Guide
A practical Ruby guide (beginner → advanced) with examples and copy buttons — ready for your blog.
1. What is Ruby?
Ruby is a dynamic, open-source programming language focused on simplicity and productivity. It has expressive syntax and powerful built-in libraries. Ruby is widely used for web apps (Rails), scripts, automation, and more.
2. Hello World
# hello.rb
puts "Hello, World!"
3. Run Ruby
- Install Ruby:
sudo apt install ruby(Linux), or userbenv/rvm. - Run a file:
ruby hello.rb - Start REPL:
irborpry(if installed).
4. Variables & Data Types
# variables.rb
name = "Amol" # String
age = 21 # Integer
pi = 3.1415 # Float
active = true # Boolean
arr = [1,2,3] # Array
h = {name: "Amol"} # Hash
nil_value = nil # nil (no value)
5. Operators & Expressions
# ops.rb
a = 10
b = 3
puts a + b # 13
puts a - b # 7
puts a * b # 30
puts a / b # 3
puts a % b # 1
# Comparisons
puts a > b # true
puts a == 10 # true
puts a != b # true
6. Control Flow
# control.rb
x = 7
if x > 10
puts "big"
elsif x >= 5
puts "medium"
else
puts "small"
end
# case
grade = 'B'
case grade
when 'A' then puts "Excellent"
when 'B' then puts "Good"
else puts "Keep trying"
end
7. Loops & Iteration
# loops.rb
# while
i = 0
while i < 3
puts i
i += 1
end
# times
3.times { |n| puts "times: #{n}" }
# for / each
for n in [1,2,3]
puts n
end
[1,2,3].each do |x|
puts "each: #{x}"
end
8. Methods (Functions)
# methods.rb
def greet(name)
"Hello, #{name}!"
end
puts greet("Amol")
# default args
def add(a, b=5)
a + b
end
puts add(10) # 15
9. Blocks, Procs & Lambdas
# blocks_procs.rb
# block example
3.times { |i| puts "block: #{i}" }
# proc
p = Proc.new { |x| puts "proc: #{x}" }
p.call(10)
# lambda
l = ->(x) { x * 2 }
puts l.call(4) # 8
Blocks are anonymous code passed to methods; Procs and lambdas are objects wrapping blocks.
10. Arrays & Hashes
# collections.rb
a = [1,2,3]
a << 4
p a # [1,2,3,4]
h = {name: "Amol", age: 21}
puts h[:name] # Amol
# common enumerable methods
a.map { |x| x * 2 } # [2,4,6,8]
a.select { |x| x.even? } # [2,4]
a.reduce(0) { |s,x| s + x } # sum
11. Classes & OOP
# person.rb
class Person
attr_accessor :name, :age
def initialize(name, age=0)
@name = name
@age = age
end
def greet
"Hello, #{@name} (#{@age})"
end
def self.species
"Homo sapiens"
end
end
p = Person.new("Amol", 21)
puts p.greet
puts Person.species
12. Modules & Mixins
# modules.rb
module Walkable
def walk
"Walking..."
end
end
class Dog
include Walkable
end
d = Dog.new
puts d.walk # Walking...
13. Exception Handling
# exceptions.rb
begin
raise "Oops!"
rescue => e
puts "Error: #{e.message}"
ensure
puts "Always runs"
end
14. File I/O
# file_io.rb
File.open("sample.txt", "w") do |f|
f.puts "Hello from Ruby"
end
txt = File.read("sample.txt")
puts txt
15. Gems & Bundler
- Install gem:
gem install sinatra - Use Bundler: create
Gemfileand runbundle install
# Gemfile example
source "https://rubygems.org"
gem "sinatra"
gem "pg"
16. Simple Web App — Sinatra
# app.rb (Sinatra)
require "sinatra"
get "/" do
"Hello from Sinatra!"
end
# Run: ruby app.rb
# Visit: http://localhost:4567
For larger web apps use Ruby on Rails.
17. Ruby on Rails (quick intro)
- Create app:
gem install railsthenrails new blog - MVC framework: Models (ActiveRecord), Views, Controllers
- Database migration:
rails db:migrate
Rails is a full-stack framework — if you want, I can make a separate Rails guide with examples.
18. Threads & Concurrency
# threads.rb
threads = []
5.times do |i|
threads << Thread.new do
sleep rand(0.1..0.5)
puts "Thread #{i} done"
end
end
threads.each(&:join)
Note: Ruby MRI has a GIL — for CPU-bound parallelism explore JRuby or processes.
19. Testing
# simple_test.rb (minitest)
require "minitest/autorun"
class TestMath < Minitest::Test
def test_add
assert_equal 4, 2 + 2
end
end
20. Useful Ruby Standard Library
net/http— HTTP clientjson— JSON parsingfileutils— File operationsdate/time— Date & Time
21. Common CLI Tips
- Run script:
ruby script.rb - Interactive:
irborpry - Check version:
ruby -v - Format code: use
rubocoporrufo
22. Example: Small CRUD in Plain Ruby (Memory)
# crud.rb
class Store
def initialize
@items = {}
@id = 0
end
def create(name)
@id += 1
@items[@id] = {id: @id, name: name}
end
def read(id)
@items[id]
end
def update(id, name)
@items[id][:name] = name if @items[id]
end
def delete(id)
@items.delete(id)
end
def all
@items.values
end
end
s = Store.new
s.create("Item A")
s.create("Item B")
p s.all
23. Security Tips
- Always validate user input
- Use parameterized queries for DB access (ActiveRecord / Sequel)
- Sanitize HTML output (e.g., Rails auto-escapes ERB)
- Keep gems updated
24. Learning Path & Resources
- Begin: learn syntax, methods, arrays & hashes
- Intermediate: OOP, blocks, enumerables, file I/O
- Web: Sinatra first, then Rails
- Advanced: concurrency, native extensions, performance
If you want, I can create a downloadable PDF handbook or a Rails guide to pair with this Ruby guide.
Ready to post — copy the whole HTML and paste into your Blogger post (HTML mode). Want this also as a PDF or a shorter cheat-sheet for readers? Reply and I’ll generate it.

Best Python Training in Pune with Placement
ReplyDeletePython Course in Pune
Python Certification Course in Pune
Python Programming Training in Pune
Advanced Python Classes in Pune