Sha256: 0a3f82cb37577e79114ab8223f9bfcd9810c655616a7e5855f0839f55a233e23

Contents?: true

Size: 957 Bytes

Versions: 20

Compression:

Stored size: 957 Bytes

Contents

#!/usr/local/bin/ruby -w
#
# == extensions/ostruct.rb
#
# Adds methods to the standard library's OpenStruct class. 
#

require "extensions/_base"
require 'ostruct'

#
# * OpenStruct#initialize
#
ExtensionsProject.implement(OpenStruct, :initialize) do
  class OpenStruct
    alias old_initialize initialize
    private :old_initialize

    #
    # Allows the initialization of an OpenStruct with a block:
    #
    #   person = OpenStruct.new do |p|
    #     p.name    = 'John Smith'
    #     p.gender  = :M
    #     p.age     = 71
    #   end 
    #
    # You can still provide a hash for initialization purposes, and even combine
    # the two approaches if you wish.
    #
    #   person = OpenStruct.new(:name => 'John Smith', :age => 31) do |p|
    #     p.gender = :M 
    #   end
    #
    def initialize(*args) # :yield: self
      old_initialize(*args)
      yield self if block_given?
    end
  end
end

Version data entries

20 entries across 20 versions & 4 rubygems

Version Path
extensions-0.6.0 lib/extensions/ostruct.rb
extensions-0.4.0 lib/extensions/ostruct.rb
extensions-0.5.0 lib/extensions/ostruct.rb
glue-0.15.0 vendor/extensions/ostruct.rb
glue-0.14.0 vendor/extensions/ostruct.rb
glue-0.16.0 vendor/extensions/ostruct.rb
glue-0.17.0 vendor/extensions/ostruct.rb
nitro-0.11.0 vendor/extensions/ostruct.rb
nitro-0.10.0 vendor/extensions/ostruct.rb
nitro-0.12.0 vendor/extensions/ostruct.rb
nitro-0.13.0 vendor/extensions/ostruct.rb
nitro-0.9.5 vendor/extensions/ostruct.rb
nitro-0.8.0 vendor/extensions/ostruct.rb
nitro-0.9.3 vendor/extensions/ostruct.rb
og-0.11.0 vendor/extensions/ostruct.rb
og-0.10.0 vendor/extensions/ostruct.rb
og-0.12.0 vendor/extensions/ostruct.rb
og-0.8.0 vendor/extensions/ostruct.rb
og-0.9.5 vendor/extensions/ostruct.rb
og-0.9.3 vendor/extensions/ostruct.rb