SlideShare a Scribd company logo
Let’s Learn Ruby - Basic
Let's Learn Ruby - Basic
Ruby Tuesday

photo by othree

https://www.facebook.com/groups/142197385837507/
RubyConf Taiwan

http://rubyconf.tw/
Rails Girls Taipei

https://www.facebook.com/railsgirlstw
WebConf Taiwan 2014
750+ attendees
all tickets sold out in 4 mins
Let’s Learn Ruby

What I want?
Let’s Learn Ruby

Problem Solving
Let’s Learn Ruby

Active Ecosystem
Let’s Learn Ruby

Scenario
Let’s Learn Ruby

open source projects on Github
Let’s Learn Ruby

History
Let’s Learn Ruby

まつもと ゆきひろ (Matz)
Let’s Learn Ruby
Let’s Learn Ruby

first released at 1995
Let’s Learn Ruby

2.0 released at 2013
Let’s Learn Ruby

2.1 released at 2013.12
Let’s Learn Ruby

Why Ruby?

free, open source, easy to learn
Let’s Learn Ruby

Ruby != Rails
Let’s Learn Ruby

Happy, and Fun
Let’s Learn Ruby

Rubies

CRuby(MRI), REE, mRuby, MacRuby, 
JRuby, IronRuby, Rubinius..etc
Let’s Learn Ruby

Version

1.8, 1.9, 2.0, 2.1
Let’s Learn Ruby

Ruby 1.8 has no future
Let’s Learn Ruby

RVM

Ruby Version Manager

https://rvm.io/
Let’s Learn Ruby

Editors

Vim, Emacs, Sublime Text... etc
Let’s Learn Ruby

coding style

https://github.com/styleguide/ruby
Let’s Learn Ruby

But Ruby is Slow..?
Let’s Learn Ruby

What can Ruby do?
Let’s Learn Ruby

Rake
Make, but Ruby version.
Rack http://rake.rubyforge.org/
Let’s Learn Ruby

Rack

it’s a specification (and implementation) of a minimal
abstract Ruby API that models HTTP.
such as Sinatra, Ruby on Rails
Rack http://rack.rubyforge.org/
Sinatra http://www.sinatrarb.com
Ruby on Rails http://rubyonrails.org/
Let’s Learn Ruby

developing MacOS and iOS app
Let’s Learn Ruby

drawing, image processing,
music..
Let’s Learn Ruby

Install Ruby now!
Let’s Learn Ruby

http://tryruby.org
Let’s Learn Ruby

Interactive Ruby, irb
Let’s Learn Ruby

Gem
Let’s Learn Ruby

gem install PACKAGE_NAME
Let’s Learn Ruby

gem env
Let’s Learn Ruby

gem list
Let’s Learn Ruby

Variables and Constants
Let’s Learn Ruby

local variable
variable
Let’s Learn Ruby

global variable
$variable
Let’s Learn Ruby

instance variable
@variable
Let’s Learn Ruby

class variable
@@variable
Let’s Learn Ruby

virtual variable
true, false, self, nil
Let’s Learn Ruby

variable assignment
a=1
x, y, z = 1, 2, 3
Let’s Learn Ruby

Constant

begins with a capital letter, 
and it can be changed
Let’s Learn Ruby

Reserved word and Keyword
Let’s Learn Ruby

Reserved word and
Keyword
Let’s Learn Ruby

Logic and Flow Control
Let’s Learn Ruby

only false and nil are false
Let’s Learn Ruby

true v.s TrueClass
false v.s FalseClass
nil v.s NilClass
Let’s Learn Ruby

if..elsif..end
Let’s Learn Ruby

unless = not if
Let’s Learn Ruby

if modifier
Let’s Learn Ruby

case .. when..
Let’s Learn Ruby

BEGIN{} and END{}
Let’s Learn Ruby

a = true ? 'a' : 'b'
Let’s Learn Ruby

a ||= 'a'
Let’s Learn Ruby

Comment
# single line
Let’s Learn Ruby

Comment

=begin .. =end
Let’s Learn Ruby

Loop and Iteration
Let’s Learn Ruby

for.. in..
Let’s Learn Ruby

while .. end
Let’s Learn Ruby

until .. end
Let’s Learn Ruby

until = not while
Let’s Learn Ruby

times
Let’s Learn Ruby

upto, downto
Let’s Learn Ruby

each, each_with_index
Let’s Learn Ruby

Integer

http://www.ruby-doc.org/core-2.1.0/Integer.html
Let’s Learn Ruby

Fixnum and Bignum
Let’s Learn Ruby

10 / 3
Let’s Learn Ruby

String

http://ruby-doc.org/core-2.1.0/String.html
Let’s Learn Ruby

single and double quotes
Let’s Learn Ruby

%q v.s %Q
Let’s Learn Ruby

"%s" % "eddie"
Let’s Learn Ruby

string interpolation
Let’s Learn Ruby

Exercise
please calculate how many “characters” and
“words” of a section of a random article with Ruby.
Let’s Learn Ruby

Exercise

please convert string “abcdefg” to “gfedcba”
without using String#reverse method.
Let’s Learn Ruby

Array

http://ruby-doc.org/core-2.1.0/Array.html
Let’s Learn Ruby

Array.new v.s []
Let’s Learn Ruby

%w
Let’s Learn Ruby

Exercise

please sort a given array [1, 3, 4, 1, 7, nil, 7],
and remove nil and duplicate number.
Let’s Learn Ruby

Exercise

please covert a given array [1, 2, 3, 4, 5] to
[1, 3, 5, 7, 9] with Array#map method.
Let’s Learn Ruby

Exercise

please draw 5 unique random number
between 1 to 52.
Let’s Learn Ruby

Hash

http://ruby-doc.org/core-2.1.0/Hash.html
Let’s Learn Ruby

Hash.new v.s {}
Let’s Learn Ruby

a = { :name => 'eddie' }
a = { name: 'eddie' }
Let’s Learn Ruby

Range

http://ruby-doc.org/core-2.1.0/Range.html
Let’s Learn Ruby

(1..10) v.s (1...10)
Let’s Learn Ruby

Exercise

please calculate the sum from 1 to 100 with
Range.
Let’s Learn Ruby

Methods
Let’s Learn Ruby

def method_name(param)
...
end
Let’s Learn Ruby

parentheses can be omitted
Let’s Learn Ruby

? and !
Let’s Learn Ruby

return value
Let’s Learn Ruby

Singleton Method
Let’s Learn Ruby

class Cat
def walk
puts "I'm walking"
end
end
!

cat = Cat.new

def cat.fly
puts "I can fly"
end

cat.fly
Let’s Learn Ruby

Method Missing
Let’s Learn Ruby

def method_missing(method_name)
puts "method: #{method_name} is called!"
end
!

something_not_exists()
Let’s Learn Ruby

Exception Handling

begin .. rescue.. else.. ensure.. end
Let’s Learn Ruby

def open_my_file(file_name)
File.open file_name do |f|
puts f.read
end
end

begin
open_my_file("block_demo.r")
rescue => e
puts e
else
puts "it's working good!"
ensure
puts "this must be executed, no matter what"
end
Let’s Learn Ruby

Block
Let’s Learn Ruby

Proc
Let’s Learn Ruby

my_square = Proc.new { | x | x ** 2 }
!

# how to call a proc
puts my_square.call(10)
puts my_square[10]
puts my_square.(10)
puts my_square === 10

#
#
#
#

100
100
100
100
Let’s Learn Ruby

lambda, ->
Let’s Learn Ruby

my_lambda = lambda { | x | x ** 2 }
!

# new style in 1.9
my_lambda = -> x { x ** 2 }
!

# how to call a lambda?
puts my_lambda.call(10)
puts my_lambda[10]
puts my_lambda.(10)
puts my_lambda === 10

#
#
#
#

100
100
100
100
Let’s Learn Ruby

Proc v.s lambda
Let’s Learn Ruby

def proc_test
puts "hello"
my_proc = Proc.new { return 1 }
my_proc.call
puts "ruby"
end
def lambda_test
puts "hello"
my_lambda = lambda { return 1 }
my_lambda.call
puts "ruby"
end
Let’s Learn Ruby

{} v.s do..end

http://blog.eddie.com.tw/2011/06/03/do-end-vs-braces/
Let’s Learn Ruby

Yield
Let’s Learn Ruby

Object-Oriented
Programming
Let’s Learn Ruby

everything in Ruby is an Object
Let’s Learn Ruby

object = state+ behavior
Let’s Learn Ruby

root class = Object

root class would be BasicObject in Ruby 1.9
Let’s Learn Ruby

class ClassName < ParentClass
...
end
Let’s Learn Ruby

Naming Convention
Let’s Learn Ruby

initialize
Let’s Learn Ruby

ClassName.new
Let’s Learn Ruby

self = current object
Let’s Learn Ruby

instance and class variable
Let’s Learn Ruby

instance and class method
Let’s Learn Ruby

Exercise
please create a Dog class and Cat class, which are
both inherited from Animal class, and implement
“walk” and “eat” methods.
Let’s Learn Ruby

public, protected and
private method
Let’s Learn Ruby

getter and setter
Let’s Learn Ruby

attr_reader, attr_writer and
attr_accessor
Let’s Learn Ruby

Open Class
Let’s Learn Ruby

Module
Let’s Learn Ruby

module ModuleName
...
end
Let’s Learn Ruby

module has no inheritance
Let’s Learn Ruby

module has no instance
Let’s Learn Ruby

Naming Convention
Let’s Learn Ruby

require v.s load
Let’s Learn Ruby

Priority?
Let’s Learn Ruby

Exercise
please create a Bird class, which is also inherited
from Animal class, but include a Fly module.
Let’s Learn Ruby

Mixin
Let’s Learn Ruby

Ruby is single inheritance
Let’s Learn Ruby

Duck Typing
Let’s Learn Ruby

include v.s extend
Let’s Learn Ruby

Bundle
Let’s Learn Ruby

Gemfile
Let’s Learn Ruby

http://rubygems.org/
Let’s Learn Ruby

gem "nokogiri", :git => "git://github.com/
tenderlove/nokogiri.git"
gem "secret_gem", :path => "~/my_secret_path"
Let’s Learn Ruby

bundle install
Let’s Learn Ruby

pack your own gem!
Let’s Learn Ruby

1. bundle gem NEW_NAME
2. gem build NEW_NAME.gemspec
3. gem push NEW_NAME.gem

http://guides.rubygems.org/make-your-own-gem/
Let’s Learn Ruby

Exercise
please try to create a Gem spec with bundle
command, modify, build and push to
rubygems.org.
Let’s Learn Ruby

Rake
Let’s Learn Ruby

desc "mail sender"
task :sendmail do
puts "grap mailing list from database..."
sleep 3
puts "mail sending..."
sleep 3
puts "done!"
end
Let’s Learn Ruby

task :goto_toliet do
puts "goto toliet"
end
!

task :open_the_door => :goto_toliet do
puts "open door"
end
Let’s Learn Ruby

TDD
Let’s Learn Ruby

require “minitest/autorun"
!

class TestMyBMI < MiniTest::Unit::TestCase
def test_my_calc_bmi_is_ok
assert_equal calc_bmi(175, 80), 26.12
end
end
!

def calc_bmi(height, weight)
bmi = ( weight / (height/100.0) ** 2 ).round(2)
end
Let’s Learn Ruby

require "minitest/autorun"

describe "test my bmi calculator" do
it "should calc the correct bmi" do
calc_bmi(175, 80).must_equal 26.12
end
end

def calc_bmi(height, weight)
bmi = ( weight / (height/100.0) ** 2 ).round(2)
end
Let’s Learn Ruby

Ruby Koans

http://rubykoans.com/
Let’s Learn Ruby

Ruby Object Model
Let’s Learn Ruby

At last..
photo by redjar
Let’s Learn Ruby

pick up one scripting language
photo by Quality & Style
Let’s Learn Ruby

@eddiekao

https://www.ruby-lang.org/zh_tw/
Let’s Learn Ruby

Ruby is fun!
Let’s Learn Ruby

The only limitation is your
imagination.
Contacts
⾼高⾒見⻯⿓龍

Website

http://www.eddie.com.tw

Blog

http://blog.eddie.com.tw

Plurk

http://www.plurk.com/aquarianboy

Facebook

http://www.facebook.com/eddiekao

Google Plus

http://www.eddie.com.tw/+

Twitter

https://twitter.com/eddiekao

Email

eddie@digik.com.tw

Mobile

+886-928-617-687

photo by Eddie

More Related Content

PPTX
Ruby Programming Language - Introduction
PDF
Introduction to Ruby
ODP
PDF
Ruby Presentation
PDF
Ruby on Rails Presentation
PPT
Ruby Basics
 
PDF
Core java kvr - satya
PPTX
ROS Based Programming and Visualization of Quadrotor Helicopters
Ruby Programming Language - Introduction
Introduction to Ruby
Ruby Presentation
Ruby on Rails Presentation
Ruby Basics
 
Core java kvr - satya
ROS Based Programming and Visualization of Quadrotor Helicopters

What's hot (20)

PDF
Introduction to systems programming
PDF
Meetup React Sanca - 29/11/18 - React Testing
PDF
Closures in Javascript
PDF
Android telephony stack
PDF
Event driven microservices
PDF
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
ODP
Basic of Java
PDF
Solid principles of oo design
PPTX
Modules in AngularJs
PPTX
Node.Js: Basics Concepts and Introduction
PPTX
Static typing vs dynamic typing languages
PDF
Hm system programming class 1
PDF
Remote Method Invocation in JAVA
PPTX
Hybrid mobile app
PPT
Programming fundamentals lecture 1&2
ODP
Introduction to Java 8
PDF
Ruby on Rails for beginners
PDF
Introduction to xcode
Introduction to systems programming
Meetup React Sanca - 29/11/18 - React Testing
Closures in Javascript
Android telephony stack
Event driven microservices
Unified Modeling Language (UML), Object-Oriented Programming Concepts & Desig...
Basic of Java
Solid principles of oo design
Modules in AngularJs
Node.Js: Basics Concepts and Introduction
Static typing vs dynamic typing languages
Hm system programming class 1
Remote Method Invocation in JAVA
Hybrid mobile app
Programming fundamentals lecture 1&2
Introduction to Java 8
Ruby on Rails for beginners
Introduction to xcode
Ad

Viewers also liked (14)

KEY
Action Controller Overview, Season 1
PPTX
Swift distributed tracing method and tools v2
PDF
Control review for iOS
PDF
September2011aftma
PPT
Ruby on Rails testing with Rspec
PPT
jQuery For Beginners - jQuery Conference 2009
PDF
Learning jQuery in 30 minutes
PDF
A swift introduction to Swift
PPTX
Web application architecture
PPT
Introduction to html
PDF
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
PPTX
Introduction to Web Architecture
PDF
jQuery and Rails: Best Friends Forever
PDF
Swift Programming Language
Action Controller Overview, Season 1
Swift distributed tracing method and tools v2
Control review for iOS
September2011aftma
Ruby on Rails testing with Rspec
jQuery For Beginners - jQuery Conference 2009
Learning jQuery in 30 minutes
A swift introduction to Swift
Web application architecture
Introduction to html
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Introduction to Web Architecture
jQuery and Rails: Best Friends Forever
Swift Programming Language
Ad

Similar to Let's Learn Ruby - Basic (20)

PDF
Zhifu Ge - How To Be Weird In Ruby - With Notes
PPTX
Ruby for PHP developers
PPT
Ruby Hell Yeah
ZIP
Meta Programming in Ruby - Code Camp 2010
PPT
Ruby for C# Developers
PPT
Intro To Ror
PDF
From nil to guru: intro to Ruby on Rails
PPTX
Ruby basics
PPTX
Ruby for .NET developers
PDF
How to discover the Ruby's defects with web application
PDF
Ruby tutorial
PPTX
Ruby :: Training 1
PDF
PDF
Introduction to Ruby & Modern Programming
PDF
Ruby Metaprogramming - OSCON 2008
PDF
my_everyday_life_with_ruby
PDF
Ruby an overall approach
PDF
Ruby — An introduction
PDF
Ruby 101
Zhifu Ge - How To Be Weird In Ruby - With Notes
Ruby for PHP developers
Ruby Hell Yeah
Meta Programming in Ruby - Code Camp 2010
Ruby for C# Developers
Intro To Ror
From nil to guru: intro to Ruby on Rails
Ruby basics
Ruby for .NET developers
How to discover the Ruby's defects with web application
Ruby tutorial
Ruby :: Training 1
Introduction to Ruby & Modern Programming
Ruby Metaprogramming - OSCON 2008
my_everyday_life_with_ruby
Ruby an overall approach
Ruby — An introduction
Ruby 101

More from Eddie Kao (20)

PDF
Rails girls in Taipei
PDF
Rails Girls in Taipei
PDF
iOS app development and Open Source
PDF
PDF
from Ruby to Objective-C
PDF
Code Reading
PDF
CreateJS - from Flash to Javascript
PDF
May the source_be_with_you
PDF
Why I use Vim
PDF
There is something about Event
PDF
Flash Ecosystem and Open Source
PDF
Happy Programming with CoffeeScript
PDF
Ruby without rails
PDF
CoffeeScript-Ruby-Tuesday
PDF
CoffeeScript
PDF
API Design
PDF
測試
PDF
3rd AS Study Group
KEY
iOS Game Development with Cocos2d
KEY
AS3讀書會(行前準備)
Rails girls in Taipei
Rails Girls in Taipei
iOS app development and Open Source
from Ruby to Objective-C
Code Reading
CreateJS - from Flash to Javascript
May the source_be_with_you
Why I use Vim
There is something about Event
Flash Ecosystem and Open Source
Happy Programming with CoffeeScript
Ruby without rails
CoffeeScript-Ruby-Tuesday
CoffeeScript
API Design
測試
3rd AS Study Group
iOS Game Development with Cocos2d
AS3讀書會(行前準備)

Recently uploaded (20)

PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Transforming Manufacturing operations through Intelligent Integrations
PPTX
Cloud computing and distributed systems.
PDF
Sensors and Actuators in IoT Systems using pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Modernizing your data center with Dell and AMD
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Advanced IT Governance
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Advanced methodologies resolving dimensionality complications for autism neur...
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Empathic Computing: Creating Shared Understanding
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Transforming Manufacturing operations through Intelligent Integrations
Cloud computing and distributed systems.
Sensors and Actuators in IoT Systems using pdf
Electronic commerce courselecture one. Pdf
Modernizing your data center with Dell and AMD
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Advanced IT Governance
Chapter 3 Spatial Domain Image Processing.pdf
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
The Rise and Fall of 3GPP – Time for a Sabbatical?
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
AI And Its Effect On The Evolving IT Sector In Australia - Elevate

Let's Learn Ruby - Basic