All Projects → arturoherrero → Ofstruct

arturoherrero / Ofstruct

Licence: mit
OpenFastStruct is a data structure, similar to an OpenStruct but faster.

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Ofstruct

Pastebin
Just another Pastebin.com CLI script, but you can even login with your user account.
Stars: ✭ 5 (-90.57%)
Mutual labels:  ruby-gem
Zache
Zero-footprint Ruby In-Memory Thread-Safe Cache
Stars: ✭ 30 (-43.4%)
Mutual labels:  ruby-gem
Dalli Rate limiter
Arbitrary Memcached-backed rate limiting for Ruby
Stars: ✭ 38 (-28.3%)
Mutual labels:  ruby-gem
Groovehq
Ruby gem for GrooveHQ api
Stars: ✭ 22 (-58.49%)
Mutual labels:  ruby-gem
Backtrace
Ruby gem to print exception backtrace nicely
Stars: ✭ 14 (-73.58%)
Mutual labels:  ruby-gem
Jekyll Pug
Jekyll Plugin That Allows You To Use Pug
Stars: ✭ 30 (-43.4%)
Mutual labels:  ruby-gem
Truemail
🚀 Configurable framework agnostic plain Ruby 📨 email validator/verifier. Verify email via Regex, DNS and SMTP. Be sure that email address valid and exists.
Stars: ✭ 717 (+1252.83%)
Mutual labels:  ruby-gem
Minimal Mistakes
📐 Jekyll theme for building a personal site, blog, project documentation, or portfolio.
Stars: ✭ 8,967 (+16818.87%)
Mutual labels:  ruby-gem
Devise Jwt
JWT token authentication with devise and rails
Stars: ✭ 881 (+1562.26%)
Mutual labels:  ruby-gem
Creds
Encrypted & plain text credentials for multiple environments
Stars: ✭ 38 (-28.3%)
Mutual labels:  ruby-gem
Gitlab
Ruby wrapper and CLI for the GitLab REST API
Stars: ✭ 939 (+1671.7%)
Mutual labels:  ruby-gem
Tty Which
Cross-platform implementation of Unix `which` command
Stars: ✭ 11 (-79.25%)
Mutual labels:  ruby-gem
Time ago in words
Humanize elapsed time from some Time instance to Time.now, e.g. '2 hours and 1 minute ago'. ATTENTION: NOT MAINTAINED
Stars: ✭ 33 (-37.74%)
Mutual labels:  ruby-gem
Omniauth Pge
OmniAuth Strategy for PG&E
Stars: ✭ 6 (-88.68%)
Mutual labels:  ruby-gem
Ruby Kafka
A Ruby client library for Apache Kafka
Stars: ✭ 1,039 (+1860.38%)
Mutual labels:  ruby-gem
Finite machine
A minimal finite state machine with a straightforward syntax.
Stars: ✭ 772 (+1356.6%)
Mutual labels:  ruby-gem
Octopoller.rb
A micro gem for polling and retrying. Perfect for making repeating requests.
Stars: ✭ 30 (-43.4%)
Mutual labels:  ruby-gem
Cucumber Api
API validator in BBD style with Cucumber
Stars: ✭ 50 (-5.66%)
Mutual labels:  ruby-gem
Power trace
Buff exception backtrace with local variables, passed in arguments and instance variables!
Stars: ✭ 48 (-9.43%)
Mutual labels:  ruby-gem
Tty Pager
Terminal output paging - cross-platform, major ruby interpreters
Stars: ✭ 37 (-30.19%)
Mutual labels:  ruby-gem

OpenFastStruct

Code Climate Build Status

OpenStruct allows the creation of data objects with arbitrary attributes.

OpenFastStruct is a data structure, similar* to an OpenStruct, that allows the definition of arbitrary attributes with their accompanying values. It benchmarks ~3x slower than a Hash, but it's ~4x faster than OpenStruct.

*An OpenFastStruct is not exactly like an OpenStruct, these are the main differences between them:

  • OpenFastStruct doesn't allow hash access like person[:name].
  • OpenFastStruct doesn't provide marshalling.
  • OpenFastStruct allows infinite chaining of attributes example.

Installation

Install the gem:

$ gem install ofstruct

Use the gem in a project managed with Bundler adding it into the Gemfile:

gem "ofstruct"

Examples

Basic usage

require "ofstruct"

person = OpenFastStruct.new
person.name = "John Smith"
person.age  = 70

puts person.name     # -> "John Smith"
puts person.age      # -> 70
puts person.address  # -> #<OpenFastStruct>

Initialize and update from a Hash

An OpenFastStruct employs a Hash internally to store the methods and values and can even be initialized or updated with one:

require "ofstruct"

person = OpenFastStruct.new(:name => "John Smith")
puts person.name  # -> "John Smith"

person.update(:name => "David Smith", :age => 70)
puts person.name  # -> "David Smith"
puts person.age   # -> 70

Remove attributes

Removing the presence of a method requires the execution the #delete_field method as setting the property value to a new empty OpenFastStruct.

require "ofstruct"

person = OpenFastStruct.new
person.name = "John Smith"
person.delete_field(:name)
puts person.name  # -> #<OpenFastStruct>

Black hole object

An OpenFastStruct instance is a black hole object that supports infinite chaining of attributes.

require "ofstruct"

person = OpenFastStruct.new
person.address.number = 4
puts person.address.number  # -> 4

Benchmarks

Probably you heard that you should never, ever use OpenStruct because the performance penalty is prohibitive. You can use OpenFastStruct instead!

Comparation between Hash, OpenFastStruct and OpenStruct:

$ ruby benchmark/hash_ofstruct_ostruct.rb
Calculating -------------------------------------
                Hash    25.518k i/100ms
      OpenFastStruct    10.527k i/100ms
          OpenStruct     3.236k i/100ms
-------------------------------------------------
                Hash    487.517k (±11.9%) i/s -      2.399M
      OpenFastStruct    159.952k (± 4.0%) i/s -    800.052k
          OpenStruct     45.602k (± 4.7%) i/s -    229.756k

Comparison:
                Hash:   487516.9 i/s
      OpenFastStruct:   159952.4 i/s - 3.05x slower
          OpenStruct:    45601.6 i/s - 10.69x slower

Comparation between RSpec Stubs, OpenFastStruct and OpenStruct:

$ ruby benchmark/double_ofstruct_ostruct.rb
Calculating -------------------------------------
         RSpec Stubs   103.000  i/100ms
      OpenFastStruct   108.000  i/100ms
          OpenStruct    45.000  i/100ms
-------------------------------------------------
         RSpec Stubs    297.809  (±17.1%) i/s -      1.545k in   5.346697s
      OpenFastStruct    262.381  (±12.2%) i/s -      1.404k in   5.430345s
          OpenStruct    185.150  (± 7.0%) i/s -    945.000

Comparison:
         RSpec Stubs:      297.8 i/s
      OpenFastStruct:      262.4 i/s - 1.14x slower
          OpenStruct:      185.2 i/s - 1.61x slower
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].