### Home Classes Methods

### In Files

- lib/facets/ostruct.rb

### Parent

Object

### Methods

- ::new
- #[]
- #[]=
- #__merge__
- #__update__
- #each
- #instance_delegate
- #ostruct_delegate
- #ostruct_merge
- #ostruct_update
- #to_h
- #to_ostruct

### Files

- README.rdoc

### Class/Module Index

Quicksearch

- Random
- Random::ArrayExtensions
- Random::HashExtensions
- Random::IntegerExtensions
- Random::NumericExtensions
- Random::RangeExtensions
- Random::StringExtensions
- Random::StringExtensions::Self
- FileUtils
- FileUtils::DryRun
- FileUtils::NoWrite
- FileUtils::Verbose
- Digest
- Digest::Class
- Digest::Instance
- Multiton
- Multiton::Inclusive
- Multiton::MetaMethods
- URI
- URI::Hash
- URI::Kernel
- Argvector
- Argvector
- ERB
- ERB::OpenTemplate
- Enumerable
- Enumerable::Arguments
- GetoptLong
- GetoptLong::DSL
- Memoizable
- Memoizable::Copy
- Numeric
- Numeric::Multipliers
- Object
- Object::ArgVector
- Timer
- Timer::Dummy
- Array
- BasicObject
- CGI
- Cloneable
- Continuation
- Date
- DateTime
- Equitable
- File
- Find
- Hash
- Instantiable
- Integer
- Interval
- Kernel
- Math
- Module
- Net
- NilClass
- NullClass
- OpEsc
- OpenStruct
- Pathname
- Platform
- Range
- RbConfig
- Set
- Shellwords
- String
- StringScanner
- Time
- Tuple
- YAML
- Zlib

No matching classes.

# OpenStruct

### Public Class Methods

new(hash=nil, &block) click to toggle source

Allows the initialization of an OpenStruct with a setter block:

    person = OpenStruct.new do |o|
      o.name    = 'John Smith'
      o.gender  = :M
      o.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

Alternatively you can provide a default block:

    stuff = OpenStruct.new{ |o,k| o[k] = [] }
    stuff.place << :a
    stuff.place << :b
    stuff.place #=> [:a, :b]

A setter block versus a defualt block is determined by the arity of the block. You can not provide both at the same time.

CREDIT: Noah Gibbs, Gavin Sinclair

    # File lib/facets/ostruct.rb, line 31
    def initialize(hash=nil, &block)
      if block && block.arity==2
        @table = Hash.new(&block)
      else
        @table = {}
      end
      if hash
        for k,v in hash
          @table[k.to_sym] = v
          new_ostruct_member(k)
        end
      end
      if block && block.arity==1
        yield self
      end
    end

### Public Instance Methods

[](key) click to toggle source

Access a value in the OpenStruct by key, like a Hash. This increases OpenStruct's "duckiness".

    o = OpenStruct.new
    o.t = 4
    o['t']  #=> 4

    # File lib/facets/ostruct.rb, line 65
    def [](key)
      key = key.to_sym unless key.is_a?(Symbol)
      @table[key]
    end

[]=(key,val) click to toggle source

Set a value in the OpenStruct by key, like a Hash.

    o = OpenStruct.new
    o['t'] = 4
    o.t  #=> 4

    # File lib/facets/ostruct.rb, line 76
    def []=(key,val)
      raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
      key = key.to_sym unless key.is_a?(Symbol)
      @table[key]=val
    end

__merge__(other) click to toggle source

Merge hash data creating a new OpenStruct object.

    o = OpenStruct.new
    x = o.ostruct_merge(:a => 2)
    x.a  #=> 2

    # File lib/facets/ostruct.rb, line 151
    def __merge__(other)
      o = dup
      o.__update__(other)
      o
    end

__update__(other) click to toggle source

Insert/update hash data on the fly.

    o = OpenStruct.new
    o.ostruct_update(:a => 2)
    o.a  #=> 2

    # File lib/facets/ostruct.rb, line 136
    def __update__(other)
      raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
      ##other = other.to_hash #to_h?
      for k,v in other
        @table[k.to_sym] = v
      end
      self
    end

each(&blk) click to toggle source

    # File lib/facets/ostruct.rb, line 49
    def each(&blk)
      @table.each(&blk)
    end

instance_delegate() click to toggle source

Provides access to an OpenStruct's inner table.

    o = OpenStruct.new
    o.a = 1
    o.b = 2
    o.instance_delegate.map { |k, v| "#{k} #{v}" }
    #=> ["a 1", "b 2"]

    # File lib/facets/ostruct.rb, line 90
    def instance_delegate
      @table
    end

Also aliased as: ostruct_delegate
ostruct_delegate() click to toggle source
Alias for: instance_delegate
ostruct_merge(other) click to toggle source

Merge hash data creating a new OpenStruct object.

    o = OpenStruct.new
    x = o.ostruct_merge(:a => 2)
    x.a  #=> 2

    # File lib/facets/ostruct.rb, line 118
    def ostruct_merge(other)
      o = dup
      o.ostruct_update(other)
      o
    end

ostruct_update(other) click to toggle source

Insert/update hash data on the fly.

    o = OpenStruct.new
    o.ostruct_update(:a => 2)
    o.a  #=> 2

    # File lib/facets/ostruct.rb, line 103
    def ostruct_update(other)
      raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
      ##other = other.to_hash  #to_h ?
      for k,v in other
        @table[k.to_sym] = v
      end
      self
    end

to_h() click to toggle source

    # File lib/facets/ostruct.rb, line 54
    def to_h
      @table.dup
    end

to_ostruct() click to toggle source

    # File lib/facets/ostruct.rb, line 158
    def to_ostruct
      self
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.