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
560
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
100
Tyrannosaurus Rx
kouphax
0
110
React
kouphax
2
690
Play for (Java|Scala)
kouphax
0
110
Devops: A Case Study
kouphax
0
67
Scala for C# Developers
kouphax
5
2.5k
Dropwizard - Production Ready Web Services
kouphax
3
1.5k
Scala for Fun & Profit
kouphax
3
610
What Agile Means To Me
kouphax
0
120
Other Decks in Programming
See All in Programming
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
160
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
190
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.3k
CSC509 Lecture 09
javiergs
PRO
0
140
Realtime API 入門
riofujimon
0
150
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
270
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
Compose 1.7のTextFieldはPOBox Plusで日本語変換できない
tomoya0x00
0
190
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Featured
See All Featured
Designing Experiences People Love
moore
138
23k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
670
RailsConf 2023
tenderlove
29
900
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Building Adaptive Systems
keathley
38
2.3k
How STYLIGHT went responsive
nonsquared
95
5.2k
Making Projects Easy
brettharned
115
5.9k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Automating Front-end Workflow
addyosmani
1366
200k
Code Review Best Practice
trishagee
64
17k
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