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
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
130
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
930
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
Remix on Hono on Cloudflare Workers
yusukebe
1
290
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.4k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
subpath importsで始めるモック生活
10tera
0
310
Contemporary Test Cases
maaretp
0
140
Featured
See All Featured
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Code Reviewing Like a Champion
maltzj
520
39k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
The Invisible Side of Design
smashingmag
298
50k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Faster Mobile Websites
deanohume
305
30k
The World Runs on Bad Software
bkeepers
PRO
65
11k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Site-Speed That Sticks
csswizardry
0
26
GraphQLとの向き合い方2022年版
quramy
43
13k
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