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 Stdlib – Minsk.rb October 2012
Search
Sergey Nartimov
October 27, 2012
Programming
10
340
Ruby Stdlib – Minsk.rb October 2012
Sergey Nartimov
October 27, 2012
Tweet
Share
More Decks by Sergey Nartimov
See All by Sergey Nartimov
PubSub at Rails
lest
0
120
Rails in production - RubyConfBY 22 Mar 2015
lest
1
140
Sequel - BRUG 21 Feb 2015
lest
0
77
Elixir – Belarus Ruby User Group 25 Jan 2014
lest
3
650
Authentication Security – RUBYSPB
lest
2
150
Geospatial applications on Rails
lest
8
390
Design patterns – Belarus Ruby on Rails User Group 23 Feb 2013
lest
8
640
Background jobs with realtime results – RailsClub'Moscow 2012
lest
5
210
Other Decks in Programming
See All in Programming
fs2-io を試してたらバグを見つけて直した話
chencmd
0
240
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
150
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
260
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
460
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
720
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
110
クリエイティブコーディングとRuby学習 / Creative Coding and Learning Ruby
chobishiba
0
3.9k
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
460
HTTP compression in PHP and Symfony apps
dunglas
2
1.7k
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
Recoilを剥がしている話
kirik
5
6.8k
Featured
See All Featured
Gamification - CAS2011
davidbonilla
80
5.1k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
How GitHub (no longer) Works
holman
311
140k
KATA
mclloyd
29
14k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Docker and Python
trallard
42
3.1k
Side Projects
sachag
452
42k
Optimising Largest Contentful Paint
csswizardry
33
3k
Designing for humans not robots
tammielis
250
25k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Facilitating Awesome Meetings
lara
50
6.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
450
Transcript
Ruby Stdlib Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest
About me • Rails, Rubinius, Elixir contributor • Software engineer
at Brainspec • Consulting, training, development Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest
Struct
Struct class Point < Struct.new(:x, :y) end
Struct class Point < Struct.new(:x, :y) end point = Point.new(4,
2)
Struct class Point < Struct.new(:x, :y) end point = Point.new(4,
2) point.x # => 4 point.y # => 2
Struct class Point < Struct.new(:x, :y) end point = Point.new(4,
2) point.x # => 4 point.y # => 2 point.z # NoMethodError
OStruct
OStruct require 'ostruct'
OStruct require 'ostruct' point = OStruct.new(x: 4, y: 2)
OStruct require 'ostruct' point = OStruct.new(x: 4, y: 2) point.x
# => 4 point.y # => 2
OStruct require 'ostruct' point = OStruct.new(x: 4, y: 2) point.x
# => 4 point.y # => 2 point.z # => nil
OStruct require 'ostruct' point = OStruct.new(x: 4, y: 2) point.x
# => 4 point.y # => 2 point.z # => nil point.z = 0
Set
Set input = [1, -2, 2, 3]
Set input = [1, -2, 2, 3] results = []
input.each do |value| results << value.abs end
Set input = [1, -2, 2, 3] results = []
input.each do |value| results << value.abs end results # => [1, 2, 2, 3]
Set input = [1, -2, 2, 3] results = []
input.each do |value| results << value.abs end results.uniq # => [1, 2, 3]
Set input = [1, -2, 2, 3] results = []
input.each do |value| results << value.abs unless ... end results # => [1, 2, 3]
Set require 'set'
Set require 'set' input = [1, -2, 2, 3]
Set require 'set' input = [1, -2, 2, 3] results
= Set.new input.each do |value| results << value.abs end
Set require 'set' input = [1, -2, 2, 3] results
= Set.new input.each do |value| results << value.abs end results # => #<Set: {1, 2, 3}>
SortedSet require 'set' set = SortedSet.new set << 1 set
<< 3 set << 2
SortedSet require 'set' set = SortedSet.new set << 1 set
<< 3 set << 2 output = [] set.each do |value| output << value end output # => [1, 2, 3]
Shellwords
Shellwords pattern = 'belongs_to :user'
Shellwords pattern = 'belongs_to :user' escaped = Shellwords.escape(pattern) system("grep -r
#{escaped} app/models")
Shellwords pattern = 'belongs_to :user' escaped = Shellwords.escape(pattern) system("grep -r
#{escaped} app/models") escaped = pattern.shellescape system("grep -r #{escaped} app/models")
Shellwords argv = ['grep', '-r', 'belongs_to :user', 'app/models']
Shellwords argv = ['grep', '-r', 'belongs_to :user', 'app/models'] system(argv.map(&:shellescape).join(' '))
Shellwords argv = ['grep', '-r', 'belongs_to :user', 'app/models'] system(argv.map(&:shellescape).join(' '))
Shellwords argv = ['grep', '-r', 'belongs_to :user', 'app/models'] system(argv.map(&:shellescape).join(' '))
system(Shellwords.join(argv))
Shellwords argv = ['grep', '-r', 'belongs_to :user', 'app/models'] system(argv.map(&:shellescape).join(' '))
system(Shellwords.join(argv)) system(argv.shelljoin)
Shellwords cmd = 'grep -r "belongs_to :user" app/ models'
Shellwords cmd = 'grep -r "belongs_to :user" app/ models' Shellwords.split(cmd)
# => ["grep", "-r", "belongs_to :user", "app/models"]
Shellwords cmd = 'grep -r "belongs_to :user" app/ models' Shellwords.split(cmd)
# => ["grep", "-r", "belongs_to :user", "app/models"] cmd.shellsplit # => ["grep", "-r", "belongs_to :user", "app/models"]
SecureRandom
SecureRandom require 'securerandom'
SecureRandom require 'securerandom' SecureRandom.base64 # => "7lM0q9QF8oYvq20WSGOmOw=="
SecureRandom require 'securerandom' SecureRandom.base64 # => "7lM0q9QF8oYvq20WSGOmOw==" SecureRandom.hex # =>
"c0cff5ead6260fe58ebd4ddab83a3497"
SecureRandom require 'securerandom' SecureRandom.base64 # => "7lM0q9QF8oYvq20WSGOmOw==" SecureRandom.hex # =>
"c0cff5ead6260fe58ebd4ddab83a3497" SecureRandom.uuid # => "4b6ff327-cb9f-43e2-a067-342072b144bd"
Delegator
Delegator class Person < Struct.new(:name) end person = Person.new('John')
Delegator class Person < Struct.new(:name) end person = Person.new('John') redditor
= Reddittor.new(person)
Delegator class Person < Struct.new(:name) end person = Person.new('John') redditor
= Reddittor.new(person) redditor.name # => 'John'
Delegator class Person < Struct.new(:name) end person = Person.new('John') redditor
= Reddittor.new(person) redditor.name # => 'John' redditor.submit_link # => 'Link by John'
Delegator require 'delegate'
Delegator class Redditor < Delegator def initialize(person) end end
Delegator class Redditor < Delegator def initialize(person) end def __setobj__(person)
@person = person end def __getobj__ @person end end
Delegator class Redditor < Delegator def initialize(person) __setobj__(person) end def
__setobj__(person) @person = person end def __getobj__ @person end end
Delegator class Redditor < Delegator def initialize(person) __setobj__(person) end def
__setobj__(person) @person = person end def __getobj__ @person end def submit_link "Link by #{name}" end end
Delegator person = Person.new('John') redditor = Redditor.new(person) redditor.name # =>
'John' redditor.submit_link # => 'Link by John'
tl;dr
Delegator class Redditor < SimpleDelegator def initialize(person) super(person) end def
submit_link "Link by #{name}" end end
Delegator person = Person.new('John') redditor = Redditor.new(person) redditor.name # =>
'John'
Delegator person = Person.new('John') redditor = Redditor.new(person) redditor.name # =>
'John' redditor.methods.grep(/name/) # => [:name, :name=]
Delegator person = Person.new('John') redditor = Redditor.new(person) redditor.name # =>
'John' redditor.methods.grep(/name/) # => [:name, :name=] Redditor.instance_methods.grep(/name/) # => []
Delegator class Redditor < DelegateClass(Person) def initialize(person) super(person) end def
submit_link "Link by #{name}" end end
Delegator class Redditor < DelegateClass(Person) def initialize(person) super(person) end def
submit_link "Link by #{name}" end end Redditor.instance_methods.grep(/name/) # => [:name, :name=]
Thanks! Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest