Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Stdlib – Minsk.rb October 2012

Ruby Stdlib – Minsk.rb October 2012

Sergey Nartimov

October 27, 2012
Tweet

More Decks by Sergey Nartimov

Other Decks in Programming

Transcript

  1. About me • Rails, Rubinius, Elixir contributor • Software engineer

    at Brainspec • Consulting, training, development Sergey Nartimov Brainspec https://github.com/lest twitter: @just_lest
  2. Struct class Point < Struct.new(:x, :y) end point = Point.new(4,

    2) point.x # => 4 point.y # => 2 point.z # NoMethodError
  3. OStruct require 'ostruct' point = OStruct.new(x: 4, y: 2) point.x

    # => 4 point.y # => 2 point.z # => nil point.z = 0
  4. Set

  5. Set input = [1, -2, 2, 3] results = []

    input.each do |value| results << value.abs end
  6. Set input = [1, -2, 2, 3] results = []

    input.each do |value| results << value.abs end results # => [1, 2, 2, 3]
  7. Set input = [1, -2, 2, 3] results = []

    input.each do |value| results << value.abs end results.uniq # => [1, 2, 3]
  8. Set input = [1, -2, 2, 3] results = []

    input.each do |value| results << value.abs unless ... end results # => [1, 2, 3]
  9. Set require 'set' input = [1, -2, 2, 3] results

    = Set.new input.each do |value| results << value.abs end
  10. Set require 'set' input = [1, -2, 2, 3] results

    = Set.new input.each do |value| results << value.abs end results # => #<Set: {1, 2, 3}>
  11. SortedSet require 'set' set = SortedSet.new set << 1 set

    << 3 set << 2 output = [] set.each do |value| output << value end output # => [1, 2, 3]
  12. Shellwords pattern = 'belongs_to :user' escaped = Shellwords.escape(pattern) system("grep -r

    #{escaped} app/models") escaped = pattern.shellescape system("grep -r #{escaped} app/models")
  13. Shellwords cmd = 'grep -r "belongs_to :user" app/ models' Shellwords.split(cmd)

    # => ["grep", "-r", "belongs_to :user", "app/models"]
  14. 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"]
  15. SecureRandom require 'securerandom' SecureRandom.base64 # => "7lM0q9QF8oYvq20WSGOmOw==" SecureRandom.hex # =>

    "c0cff5ead6260fe58ebd4ddab83a3497" SecureRandom.uuid # => "4b6ff327-cb9f-43e2-a067-342072b144bd"
  16. Delegator class Person < Struct.new(:name) end person = Person.new('John') redditor

    = Reddittor.new(person) redditor.name # => 'John' redditor.submit_link # => 'Link by John'
  17. Delegator class Redditor < Delegator def initialize(person) __setobj__(person) end def

    __setobj__(person) @person = person end def __getobj__ @person end end
  18. 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
  19. Delegator person = Person.new('John') redditor = Redditor.new(person) redditor.name # =>

    'John' redditor.methods.grep(/name/) # => [:name, :name=]
  20. Delegator person = Person.new('John') redditor = Redditor.new(person) redditor.name # =>

    'John' redditor.methods.grep(/name/) # => [:name, :name=] Redditor.instance_methods.grep(/name/) # => []
  21. 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=]