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 2.0 Walkthrough: The Best Bits
Search
Peter Cooper
February 25, 2013
Programming
11k
67
Share
Ruby 2.0 Walkthrough: The Best Bits
A quick overview of a few of the best bits in Ruby 2.0.
Peter Cooper
February 25, 2013
More Decks by Peter Cooper
See All by Peter Cooper
The Future of Code
peterc
0
150
Redis Steady Go (Nov 2012)
peterc
18
1.2k
I Choo-Choo Choose The Web: A History of the Web and JavaScript 1945-2012
peterc
1
220
Redis Steady Go
peterc
31
2.2k
Other Decks in Programming
See All in Programming
作って学ぶ、 JSX (TSX) ランタイムの基本
syumai
7
1.5k
DynamoDBには集計系のクエリがないけどなんとかしたい
musan
1
130
ビジネスモデルから紐解く、AI+型駆動開発
hirokiomote
2
5.2k
AI時代のUIはどこへ行く?その2!
yusukebe
19
6.6k
net-httpのHTTP/2対応について
naruse
0
440
Signal Forms: Beyond the Basics @ngBaguette 2026 in Paris
manfredsteyer
PRO
0
220
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.4k
SPMマルチモジュールで テストカバレッジを取得する技法
yosshi4486
0
140
oxlintはeslint/typescript-eslintを置き換えられるのか
shomafujita
2
310
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
240
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
210
AIチームを指揮するOSS「TAKT」活用術 / How to Use “TAKT,” an OSS Tool for Orchestrating AI Teams
nrslib
6
820
Featured
See All Featured
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.3k
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Fireside Chat
paigeccino
42
3.9k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
1.6k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
270
How to make the Groovebox
asonas
2
2.2k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
The Cult of Friendly URLs
andyhume
79
6.9k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
410
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Facilitating Awesome Meetings
lara
57
6.9k
Transcript
RUBY 2.0 WALKTHROUGH
The Best Bits THE RUBY 2.0 RUNTHROUGH
A sprint, not a stroll
BIG PARTS
Refinements Experimental scope-limited monkey patching
module FloatDivision refine Fixnum do def /(other); self.to_f / other;
end end end class MathFun using FloatDivision def self.ratio(a, b) a / b end end p MathFun.ratio(6, 8) THE OLD IDEA (no longer works)
module FloatDivision refine Fixnum do def /(other); self.to_f / other;
end end end class MathFun using FloatDivision def self.ratio(a, b) a / b end end p MathFun.ratio(6, 8)
module FloatDivision refine Fixnum do def /(other); self.to_f / other;
end end end using FloatDivision class MathFun def self.ratio(a, b) a / b end end p MathFun.ratio(6, 8) Main context is OK
Keyword Arguments definitely a: “pretty”, good: “addition”, to: “the language”
def some_method(options = {}) defaults = { x: 10, y:
20, z: 30 } options = defaults.merge(options) p options end some_method x: 1, y: 2
def some_method(x: 10, y: 20, z: 30) p x, y,
z end some_method x: 1, y: 2, z: 3
def some_method(x: 10, **rest) p x, rest end some_method x:
1, y: 2, z: 3, c: “x”
Enumerator::Lazy Lazy enumeration, finally made easy.
infinite_range = (0..Float::INFINITY) infinite_range.select { |num| num % 74 ==
0 }
infinite_range = (0..Float::INFINITY) e = infinite_range.lazy.select { |num| num %
74 == 0 } puts e.next puts e.next puts e.next
Module#prepend
module Bar def my_method "inside the module" end end class
Foo include Bar def my_method "inside the class" super end end x = Foo.new p x.my_method 1 2
module Bar def my_method "inside the module" super end end
class Foo prepend Bar def my_method "inside the class" end end x = Foo.new p x.my_method 1 2
INCLUDE PREPEND Foo class Bar module x object NEITHER Foo
class x object Object class Object class Foo class Bar module x object Object class
DETAILS
ABI Breakage Changes to the Application Binary Interface
Regex engine changed From Oniguruma to Onigmo
%{this\r\nis\n\ncool\vhello!}.gsub(/\R/, '')
RubyGems 2.0 Lots of refactoring, initial support for stdlib gemification,
gem search is now remote, arbitrary metadata support & more.
RDoc 4.0 Adds Markdown support & ri can now show
pages (e.g. READMEs with ri rdoc:README)
%i and %I To form arrays of symbols, like %w
does for words and arrays
%i{a b c} == [:a, :b, :c] %I{ #{(rand(26) +
65).chr} } == [:Y]
UTF-8 is default source encoding No more # encoding: utf-8
(if you don’t want)
Bitmap garbage collector Faster, more efficient
CSV.load and CSV.dump gone Not considered such a good idea
in light of the early 2013 YAML vulnerabilities
String#chars, #lines, #codepoints, etc. Now return arrays rather than enumerators.
Use #each_char, #each_line, etc. instead if you need enumerators.
TracePoint A more object oriented alternative to set_trace_func
tracer = lambda do |event, file, line, id, binding, klass|
to_display = [event, File.basename(file), line, klass, id] puts "%10s in %s at line %-2d %s:%s" % to_display end set_trace_func tracer # .. normal code here ..
tracer = TracePoint.new do |tp| to_display = [tp.event, File.basename(tp.path), tp.lineno,
tp.defined_class, tp.method_id] puts "%10s in %s at line %-2d %s:%s" % to_display end tracer.enable # .. normal code here ..
respond_to? respond_to? against a protected method now returns false
Method transplants with define_method Now accepts UnboundMethods
module M def foo; "foo"; end end define_method :foo, M.instance_method(:foo)
p foo
Array#bsearch Range#bsearch Binary search for monotonic collections
[1, 4, 9, 13, 14, 22, 40].bsearch { |i| p
i; i >= 6 } # 13 # 4 # 9 # => 9 (1..1000).bsearch { |i| i > 372 }
__dir__ Like __FILE__ but for the current source file’s directory
Equivalent to File.dirname(File.realpath(__FILE__))
to_h and Hash() A new convention. The hash equivalent to
#to_a A key use is with Struct and OpenStruct
User = Struct.new(:name, :age, :status) me = User.new("Peter", 31, :admin)
me.to_h ENV.to_h Hash(nil) # => {} Hash([]) # => {}
FIN https://cooperpress.com/