Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby by Example
Search
James Hughes
June 22, 2012
Programming
7
580
Ruby by Example
James Hughes
June 22, 2012
Tweet
Share
More Decks by James Hughes
See All by James Hughes
Functional Programming with Clojure
kouphax
1
130
Tyrannosaurus Rx
kouphax
0
120
React
kouphax
2
720
Play for (Java|Scala)
kouphax
0
120
Devops: A Case Study
kouphax
0
80
Scala for C# Developers
kouphax
5
2.6k
Dropwizard - Production Ready Web Services
kouphax
3
1.6k
Scala for Fun & Profit
kouphax
4
640
What Agile Means To Me
kouphax
0
140
Other Decks in Programming
See All in Programming
CSC305 Lecture 02
javiergs
PRO
1
260
あなたの知らない「動画広告」の世界 - iOSDC Japan 2025
ukitaka
0
430
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
190
Go言語の特性を活かした公式MCP SDKの設計
hond0413
1
200
After go func(): Goroutines Through a Beginner’s Eye
97vaibhav
0
240
Advance Your Career with Open Source
ivargrimstad
0
390
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
430
そのpreloadは必要?見過ごされたpreloadが技術的負債として爆発した日
mugitti9
2
3.1k
Reduxモダナイズ 〜コードのモダン化を通して、将来のライブラリ移行に備える〜
pvcresin
2
690
2分台で1500examples完走!爆速CIを支える環境構築術 - Kaigi on Rails 2025
falcon8823
3
3.4k
Django Ninja による API 開発効率化とリプレースの実践
kashewnuts
0
1.1k
私はどうやって技術力を上げたのか
yusukebe
43
18k
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
53
7.8k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
890
How STYLIGHT went responsive
nonsquared
100
5.8k
Rails Girls Zürich Keynote
gr2m
95
14k
Scaling GitHub
holman
463
140k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
51k
Building an army of robots
kneath
306
46k
Visualization
eitanlees
148
16k
Producing Creativity
orderedlist
PRO
347
40k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
1.6k
Side Projects
sachag
455
43k
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.7k
Transcript
RUBY BY EXAMPLE
Ruby is... interpreted object oriented dynamic
Ruby is... productive intuitive popular
VARIABLES & TYPES
Before we begin... a.+(b) a.is_valid? a.decrement!
$counter @@counter @counter counter Global Class Instance Local
‘hello’ ‘’‘hello’’’ %q{hello} String Herestring Perl-inspired
a = ‘James’ b = “Hello #{a}” puts b =>
“Hello James”
:hello Symbol
puts <<DOC This is a "heredoc" Multi-line String DOC
[1,2,3] {:a=>1, :b=>2} {a: 1, b: 2} 0..10 Array Hash
(1.8) Hash (1.9) Ranges
METHODS
def greet(name) “Hello, #{name}” end greet “James”
def greet(name=“World”) “Hello, #{name}” end greet “James” greet
CONTROL FLOW
if rating >= 4 puts "great" elsif rating == 3
puts "alright" else puts "sucks" end
unless rating == 5 puts “Try harder” end
puts “Try harder” unless rating == 5 puts “Seriously” if
rating == 1
case rating when 4..5 puts "great" when 3 puts "alright"
else puts "sucks" end
while !le.has_more_lines? puts !le.next_line end puts !le.next_line while !le.has_more_lines?
until !le.end_of_!le? puts !le.next_line end puts !le.next_line until !le.end_of_!le?
for i in 0...!le.line_count puts !le.lines[i] end
BLOCKS
[1,2,3,4].each do |i| puts i end
array.each(&block)
printer = Proc.new do |i| puts i end [1,2,3,4].each &printer
printer = lambda { |i| puts i }
def loggerWrapper puts "Executing Method" yield puts "Done Executing" end
loggerWrapper { puts "Weeee!" }
COLLECTIONS
Array/Enumerable Operations 109 Hash Operations 50
[1,2,3,4,5].map { |i| i +1} [1,2,3,4,5].reduce { |i, j| i
+ j } ({a: '1'}).merge({b: '2'})
CLASSES
class Person def initialize(name, age) @name,@age = name,age end end
person = Person.new("James", 32)
class Person attr_accessor :name attr_accessor :age ... end p =
Person.new("James", 32) puts p.name, p.age
class Person ... end class Hero < Person attr_accessor :powers
end p = Hero.new("James", 32) p.powers = [“Flying”]
MODULES & MIXINS
module StrUtils def self.salt(pass, slt) “#{pass}#{salt}” end def salt(slt) StrUtils::salt(@value,
slt) end end
StrUtils::salt("123", "456")
class Password include Utils def initialize(value) @value = value end
end Password.new("test").salt
TOOLS OF THE TRADE
irb Interactive shell ruby Ruby interpreter gem Library manager rake
Build tool rails Web framework
RUBY BY EXAMPLE