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
110
Rails in production - RubyConfBY 22 Mar 2015
lest
1
140
Sequel - BRUG 21 Feb 2015
lest
0
75
Elixir – Belarus Ruby User Group 25 Jan 2014
lest
3
640
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
Vertical Architectures for Scalable Angular Applications
manfredsteyer
PRO
0
270
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
13
3.6k
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
2
2.6k
WEBエンジニア向けAI活用入門
sutetotanuki
0
280
gopls を改造したら開発生産性が高まった
satorunooshie
8
220
Amazon Neptuneで始めてみるグラフDB-OpenSearchによるグラフの全文検索-
satoshi256kbyte
4
290
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
290
tc39 x jsconf.jp Panel Discussion 2024
yosuke_furukawa
PRO
0
130
レイトレ合宿10 レンダラー紹介 / Renderer Introduction, Ray Tracing Camp 10
shocker_0x15
0
680
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
220
いかにして不足・不整合なくデータ移行したか
tjmtmmnk
1
1k
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.2k
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3.5k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.2k
Become a Pro
speakerdeck
PRO
24
4.9k
RailsConf 2023
tenderlove
29
870
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Statistics for Hackers
jakevdp
796
220k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
770
Building Adaptive Systems
keathley
38
2.2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Being A Developer After 40
akosma
86
590k
The Cost Of JavaScript in 2023
addyosmani
45
6k
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
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