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
talk-with-local-llm-with-web-streams-api
kbaba1001
0
180
良いユニットテストを書こう
mototakatsu
7
2.2k
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
200
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
110
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
Haze - Real time background blurring
chrisbanes
1
510
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
270
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
php-conference-japan-2024
tasuku43
0
270
テスト自動化失敗から再挑戦しチームにオーナーシップを委譲した話/STAC2024 macho
ma_cho29
1
1.3k
RWC 2024 DICOM & ISO/IEC 2022
m_seki
0
210
Featured
See All Featured
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Making the Leap to Tech Lead
cromwellryan
133
9k
How to Ace a Technical Interview
jacobian
276
23k
For a Future-Friendly Web
brad_frost
175
9.4k
The Invisible Side of Design
smashingmag
298
50k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Docker and Python
trallard
42
3.1k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Adopting Sorbet at Scale
ufuk
73
9.1k
Git: the NoSQL Database
bkeepers
PRO
427
64k
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