Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

jubishop/rstruct

Repository files navigation

RStruct

RSpec Status Rubocop Status

A cleaner, simpler version of Ruby Structs.

Installation

In a Gemfile

source: 'https://www.jubigems.org/' do
 gem 'rstruct'
end

Usage

RStruct provides two class definitions: RStruct and KVStruct.

RStruct

RStruct defines a Struct that takes a flat list of parameters to its constructor.

All required params: MyStruct = RStruct.new(:one, :two, :three)

All optional params: MyStruct = RStruct.new([:one, :two, :three])

First param required: MyStruct = RStruct.new(:one, [:two, :three])

You can of course open the classes to define additional functions, just like normal Structs:

MyStruct = RStruct.new(:one, %i[two three]) {
 def say_hi
 "Hello"
 end
}

Creating a MyStruct works as you'd expect:

my_instance = MyStruct.new(1) # :two and :three are optional
my_instance.say_hi # "Hello"

If you want to define default values for the optional params, you can override initialize:

MyStruct = RStruct.new(:one, %i[two three]) {
 def initialize(one, two = 2, three = nil)
 super(one, two, three)
 end
}

Now when you create a mystruct, .two will have the default value 2, but .three will still be nil:

myInstance = MyStruct.new(1)
myInstance.two # 2
myInstance.three # nil

KVStruct

KVStruct defines a Struct that takes key value pairs.

All required params: MyStruct = KVStruct.new(:one, :two, :three)

All optional params: MyStruct = KVStruct.new([:one, :two, :three])

First param required: MyStruct = KVStruct.new(:one, [:two, :three])

Creating these now requires key-value pairs:

myInstance = MyStruct.new(one: 1) # :two and :three are optional

You could also define defaults for these in initialize too:

MyStruct = KVStruct.new(:one, :two, [:three]) {
 def initialize(one:, two:, three: 'three')
 super(one: one, two: two, three: three)
 end
}

then:

myInstance = KVStruct(one: 1, two: 2)
myInstance.three # "three"

You can also define the defaults directly to the args as a Hash:

MyStruct = KVStruct.new(:one, :two, [:three]) {
 def initialize(args)
 args[:three] = args.fetch(:three, "three")
 super(args)
 end
}

License

The gem is available as open source under the terms of the MIT License.

About

A cleaner, simpler version of Ruby Structs.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /