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
330
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
380
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
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
480
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
2k
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
610
Arm移行タイムアタック
qnighy
0
330
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Featured
See All Featured
The Pragmatic Product Professional
lauravandoore
31
6.3k
The Cult of Friendly URLs
andyhume
78
6k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Optimizing for Happiness
mojombo
376
70k
Building Your Own Lightsaber
phodgson
103
6.1k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Designing for humans not robots
tammielis
250
25k
Side Projects
sachag
452
42k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
How to Ace a Technical Interview
jacobian
276
23k
How to train your dragon (web standard)
notwaldorf
88
5.7k
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