This document introduces Ruby programming concepts through code examples. It covers Ruby basics like variables, numbers, strings, symbols, constants, methods, arrays, hashes, classes and modules. It also discusses the Ruby ecosystem including RVM/rbenv, RubyGems, Bundler and Git/GitHub. The document compares Ruby and Ruby on Rails, lists popular editors for Ruby and provides references for further reading.
9. Ruby Philosophie
“I believe people want to express themselves
when they program. They don’t want to fight
with the language. Programming languages
must feel natural to programmers. I tried to
make people enjoy programming and
concentrate on the fun and creative part of
programming when they use Ruby.”
-- Yukihiro "Matz" Matsumoto, Ruby creator
10. Why Ruby?
• Its simple
• Easy to write/learn
• Truly object oriented
• Emphasize programming speed!
• Less code, fewer bugs
• Open source
11. From where to start?!
Installation
• Just google with install ruby with
rvm/rbenv in <Your OS>
• Should be simple as like Ruby
12. From where to start?!
IRB = Interactive Ruby
> Irb
Or
tryruby.org
17. It doesn’t mind
• if you don’t like ‘;’ at the end
• even a method without ()
• If you want to express in your way
• and many more..
Happy Programmer
18.
19.
20. Variables
• Any plain, lowercase word
• a, my_variable and rubyconf2017
• > a => undefined local variable or method `a`
• > a = ‘hello’ => hello
• > a => hello
• > a = 1 => 1
28. Classes
class Car
attr_accessor :wheels, :color
end
c = Car.new => #<Car:0x00000107b09840>
c2 = Car.new =>#<Car:0x00000107b00a60>
c.color = ‘white’ => white
c.color => white
c2.color => nil
29. class Car
attr_accessor :wheels, :color
def initialize(wheels, color)
self. wheels = wheels
self. color = color
end
def color_and_wheels
“#{color} color with #{wheels} ‘wheels’)”
end
end
c = Car.new(4, ‘white’) => #<Car0x00000b09840 .....>
c.color_and_wheels => ‘white color with 4 wheels’
30. Classes (Open to modify)
class Bird
def make_sound
p “ka-ka-ka”
end
def color
p “black”
end
end
b = Bird.new
b.make_sound => ‘ka-ka-ka’
b.color => ‘black’
31. Classes (Open to modify)
class Bird
def make_sound
p “kuhu :D ”
end
def name
p “a name”
end
b = Bird.new
b.make_sound => ‘kuhu :D’
b.color => ‘black’
b.name => ‘a name’
Don’t like the sound? Just open the class and
modify it
32. Module
Like classes, modules contain methods and
constants but don’t have instances.
encourage modular design
Ruby doesn’t support multiple inheritance
but can have that behavior by modules
34. Module
class Thing < Parent
include MathFunctions
include Taggable
include Persistence
end
35. module Feature
def feature1
p ‘feature1’
end
def feature2
p ‘feature2’
end
end
module Habits
def habit1
p ‘habit1’
end
def habit2
p ‘habit2’
end
end
class Parent
def a1
p ‘a1’
end
end
class Thing < Parent
include Feature
include Habits
end
t = Thing.new
t.a1 => ‘a1’
t.feature1 => ‘feature1’
t.habit2 => ‘habit2’
36. More on Ruby Basics
• http://rubymonk.com/
• http://tryruby.org/
• http://rubykoans.com/
• http://ruby.learncodethehardway.org/
• http://poignant.guide/ (funny!)
38. Ruby vs Ruby on Rails(aka Rails)
• Rails is written in the Ruby
• Rails uses many Ruby gems
• Rails is a framework
• Rails is used to build web apps
rubyonrails.org