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 Tricks 2
Search
Michał Łomnicki
November 21, 2012
Technology
3
60
Ruby Tricks 2
Michał Łomnicki
November 21, 2012
Tweet
Share
More Decks by Michał Łomnicki
See All by Michał Łomnicki
DDD, Rails and persistence
mlomnicki
1
270
Forget Ruby. Forget CoffeeScript. Do SOA
mlomnicki
1
120
Having fun with legacy apps
mlomnicki
1
62
CAP Theorem
mlomnicki
2
120
[PL] Transakcje w bazach danych
mlomnicki
1
170
SchemaPlus
mlomnicki
1
40
Other Decks in Technology
See All in Technology
リーダブルテストコード 〜メンテナンスしやすい テストコードを作成する方法を考える〜 #DevSumi #DevSumiB / Readable test code
nihonbuson
11
6.8k
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
6
57k
スクラムのイテレーションを導入してチームの雰囲気がより良くなった話
eccyun
0
110
データの品質が低いと何が困るのか
kzykmyzw
6
1.1k
Culture Deck
optfit
0
390
『衛星データ利用の方々にとって近いようで触れる機会のなさそうな小話 ~ 衛星搭載ソフトウェアと衛星運用ソフトウェア (実物) を動かしながらわいわいする編 ~』 @日本衛星データコミニティ勉強会
meltingrabbit
0
140
Datadogとともにオブザーバビリティを布教しよう
mego2221
0
130
【Developers Summit 2025】プロダクトエンジニアから学ぶ、 ユーザーにより高い価値を届ける技術
niwatakeru
2
1.2k
利用終了したドメイン名の最強終活〜観測環境を育てて、分析・供養している件〜 / The Ultimate End-of-Life Preparation for Discontinued Domain Names
nttcom
1
120
Nekko Cloud、 これまでとこれから ~学生サークルが作る、 小さなクラウド
logica0419
2
880
Postman Flowsの基本 / Postman Flows Basics
yokawasa
1
100
スタートアップ1人目QAエンジニアが QAチームを立ち上げ、“個”からチーム、 そして“組織”に成長するまで / How to set up QA team at reiwatravel
mii3king
2
1.3k
Featured
See All Featured
A better future with KSS
kneath
238
17k
Measuring & Analyzing Core Web Vitals
bluesmoon
6
240
GitHub's CSS Performance
jonrohan
1030
460k
Typedesign – Prime Four
hannesfritz
40
2.5k
Practical Orchestrator
shlominoach
186
10k
The Cost Of JavaScript in 2023
addyosmani
47
7.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
114
50k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
Six Lessons from altMBA
skipperchong
27
3.6k
Designing for Performance
lara
604
68k
How GitHub (no longer) Works
holman
313
140k
Transcript
Ruby Tips & Quirks #2 Michał Łomnicki http://mlomnicki.com
Local variables puts local_variables.inspect y = 4 puts local_variables.inspect
Local variables puts local_variables.inspect # => ["y"] y = 4
puts local_variables.inspect # => ["y"]
Local variables Ruby 1.8 puts local_variables.inspect # => ["y"] y
= 4 puts local_variables.inspect # => ["y"]
Local variables Ruby 1.9 puts local_variables.inspect # => [:y] y
= 4 puts local_variables.inspect # => [:y]
String vs Symbol class String unless instance_methods.include?("camelize") def camelize gsub(/\/(.?)/)
{ "::#{$1.upcase}" } .gsub(/(?:^|_)(.)/) { $1.upcase } end end end
String vs Symbol class String unless instance_methods.any? { |m| m.to_s
== "camelize" def camelize gsub(/\/(.?)/) { "::#{$1.upcase}" } .gsub(/(?:^|_)(.)/) { $1.upcase } end end end
String vs Symbol class String unless instance_methods.method_defined?(:camelize) def camelize gsub(/\/(.?)/)
{ "::#{$1.upcase}" } .gsub(/(?:^|_)(.)/) { $1.upcase } end end end
module functions module Security def generate_password ('a'..'z').sample(8) end end class
User include Security end User.new.generate_password
module functions module Security def generate_password ('a'..'z').sample(8) end end
module functions module Security extend self def generate_password ('a'..'z').sample(8) end
end
module functions module Security extend self def generate_password ('a'..'z').sample(8) end
end Security.generate_password
module functions module Security module_function def generate_password ('a'..'z').sample(8) end end
Security.generate_password
respond_to?(:super) module Sanitizer def save puts "sanitized" super end end
module Persistance def save puts "saved" super end end class User include Persistance include Sanitizer end
respond_to?(:super) module Sanitizer def save puts "sanitized" super end end
module Persistance def save puts "saved" super end end class User include Persistance include Sanitizer end
respond_to?(:super) > User.new.save => sanitized => saved => save: super:
no superclass method save (NoMeth
respond_to?(:super) module Sanitizer def save puts "sanitized" super if respond_to?(:super)
end end module Persistance def save puts "saved" super if respond_to?(:super) end end class User include Persistance include Sanitizer end
respond_to?(:super) module Sanitizer def save puts "sanitized" super if respond_to?(:super)
end end module Persistance def save puts "saved" super if respond_to?(:super) end end class User include Persistance include Sanitizer end
respond_to?(:super) > User.new.save => sanitized
respond_to?(:super) module Sanitizer def save puts "sanitized" super if defined?(super)
end end module Persistance def save puts "saved" super if defined?(super) end end class User include Persistance include Sanitizer end
respond_to?(:super) > User.new.save => sanitized => saved
Class.include module Sanitizer def save puts "sanitized" super if defined(super)
end end module Persistance def save puts "saved" super if defined(super) end end class User include Persistance include Sanitizer end
Class.include module Sanitizer def save puts "sanitized" super if defined(super)
end end module Persistance def save puts "saved" super if defined(super) end end class User include Persistance, Sanitizer end
Class.include > User.new.save => saved => sanitized
Class.include class User include Persistance, Sanitizer end
Class.include class User include Sanitizer, Persistance end
Class.include > User.new.save => sanitized => saved
Block comments =begin Objects don't specify their attributes directly, but
rather infer them from the table definition with which they're linked. Adding, removing, and changing attributes and their type is done directly in the database. =end
Ruby1.9 - each_with_object > (1..5).inject({}) do |i, hsh| hsh[i] =
i*2 hsh end => {1=>2, 2=>4, 3=>6, 4=>8, 5=>10} VS > (1..5).each_with_object({}) do |i, hsh| hsh[i] = i*2 end => {1=>2, 2=>4, 3=>6, 4=>8, 5=>10}
Ruby1.9 - public_send class User protected def destroy puts "destroyed"
end end User.new.public_send(:destroy) NoMethodError: protected method destroy called
Ruby1.9 - ObjectSpace.count_objects ObjectSpace.count_objects { :TOTAL=>76928, :FREE=>549, :T_OBJECT=>1363, :T_CLASS=>1008, :T_MODULE=>38,
:T_FLOAT=>7, :T_STRING=>50339, :T_REGEXP=>234, :T_ARRAY=>7259, :T_HASH=>558, :T_FILE=>16, :T_DATA=>1695, }
Ruby1.9 define_finalizer str = "ruby1.9" ObjectSpace.define_finalizer(str) do |object_id| puts "string
was destroyed id: #{object_id}" end str = nil GC.start => string was destroyed id: -607935038
Ruby1.9 call proc prc = proc { puts "proc called"
} 1) prc.call(1) # 1.8 2) prc[2] # 1.8 3) prc.(3) # new 4) prc.===(4) # new
Ruby1.9 call proc sleep_time = proc do |time| case time.hour
when 0..6 then true else false end end case Time.now when sleep_time puts "go to bed. now!" else puts "work harder" end
Ruby1.9 call proc sleep_time = proc do |time| case time.hour
when 0..6 then true else false end end case Time.now when sleep_time puts "go to bed. now!" else puts "work harder" end sleep_time.===(Time.now)
Questions?