How to change the default value of a Struct attrib

2020-02-10 11:20发布

According to the documentation unset attributes of Struct are set to nil:

unset parameters default to nil.

Is it possible to specify the default value for particular attributes?

For example, for the following Struct

Struct.new("Person", :name, :happy)

I would like the attribute happy to default to true rather than nil. How can I do this? If I do as follows

Struct.new("Person", :name, :happy = true)

I get

-:1: syntax error, unexpected '=', expecting ')'
Struct.new("Person", :name, :happy = true)
                                    ^
-:1: warning: possibly useless use of true in void context

标签: ruby
7条回答
Bombasti
2楼-- · 2020-02-10 11:39

You can do this with a singleton:

Person = Struct.new("Person", :name, :happy) do
  @@defaults = {:happy => true}
  @@vars = [:name, :happy]
  def [](i)
    return super if(super)
    return @@defaults[i] if(@@defaults[i])
    return nil
  end
  @@vars.each do |v|
    define_method(v) {return self[v]}
  end
end
查看更多
霸刀☆藐视天下
3楼-- · 2020-02-10 11:46

This can also be accomplished by creating your Struct as a subclass, and overriding initialize with default values as in the following example:

class Person < Struct.new(:name, :happy)
    def initialize(name, happy=true); super end
end

On one hand, this method does lead to a little bit of boilerplate; on the other, it does what you're looking for nice and succinctly.

One side-effect (which may be either a benefit or an annoyance depending on your preferences/use case) is that you lose the default Struct behavior of all attributes defaulting to nil -- unless you explicitly set them to be so. In effect, the above example would make name a required parameter unless you declare it as name=nil

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-02-10 11:47

@Linuxios gave an answer that overrides member lookup. This has a couple problems: you can't explicitly set a member to nil and there's extra overhead on every member reference. It seems to me you really just want to supply the defaults when initializing a new struct object with partial member values supplied to ::new or ::[].

Here's a module to extend Struct with an additional factory method that lets you describe your desired structure with a hash, where the keys are the member names and the values the defaults to fill in when not supplied at initialization:

# Extend stdlib Struct with a factory method Struct::with_defaults
# to allow StructClasses to be defined so omitted members of new structs
# are initialized to a default instead of nil
module StructWithDefaults

  # makes a new StructClass specified by spec hash.
  # keys are member names, values are defaults when not supplied to new
  #
  # examples:
  # MyStruct = Struct.with_defaults( a: 1, b: 2, c: 'xyz' )
  # MyStruct.new       #=> #<struct MyStruct a=1, b=2, c="xyz"
  # MyStruct.new(99)   #=> #<struct MyStruct a=99, b=2, c="xyz">
  # MyStruct[-10, 3.5] #=> #<struct MyStruct a=-10, b=3.5, c="xyz">
  def with_defaults(*spec)
    new_args = []
    new_args << spec.shift if spec.size > 1
    spec = spec.first
    raise ArgumentError, "expected Hash, got #{spec.class}" unless spec.is_a? Hash
    new_args.concat spec.keys

    new(*new_args) do

      class << self
        attr_reader :defaults
      end

      def initialize(*args)
        super
        self.class.defaults.drop(args.size).each {|k,v| self[k] = v }
      end

    end.tap {|s| s.instance_variable_set(:@defaults, spec.dup.freeze) }

  end

end

Struct.extend StructWithDefaults
查看更多
爷的心禁止访问
5楼-- · 2020-02-10 11:51

I think that the override of the #initialize method is the best way, with call to #super(*required_args).

This has an additional advantage of being able to use hash-style arguments. Please see the following complete and compiling example:

Hash-Style Arguments, Default Values, and Ruby Struct

# This example demonstrates how to create Ruby Structs that use
# newer hash-style parameters, as well as the default values for
# some of the parameters, without loosing the benefits of struct's
# implementation of #eql? #hash, #to_s, #inspect, and other
# useful instance methods.
#
# Run this file as follows
#
# > gem install rspec
# > rspec struct_optional_arguments.rb --format documentation
#
class StructWithOptionals < Struct.new(
    :encrypted_data,
    :cipher_name,
    :iv,
    :salt,
    :version
    )

    VERSION = '1.0.1'

    def initialize(
        encrypted_data:,
        cipher_name:,
        iv: nil,
        salt: 'salty',
        version: VERSION
        )
        super(encrypted_data, cipher_name, iv, salt, version)
    end
end

require 'rspec'
RSpec.describe StructWithOptionals do
    let(:struct) { StructWithOptionals.new(encrypted_data: 'data', cipher_name: 'AES-256-CBC', iv: 'intravenous') }

    it 'should be initialized with default values' do
        expect(struct.version).to be(StructWithOptionals::VERSION)
    end

    context 'all fields must be not null' do
        %i(encrypted_data cipher_name salt iv version).each do |field|
            subject { struct.send(field) }
            it field do
                expect(subject).to_not be_nil
            end
        end
    end
end
查看更多
▲ chillily
6楼-- · 2020-02-10 12:00

I also found this:

Person = Struct.new "Person", :name, :happy do
  def initialize(*)
    super
    self.location ||= true
  end
end
查看更多
Evening l夕情丶
7楼-- · 2020-02-10 12:02

Just add another variation:

class Result < Struct.new(:success, :errors)
  def initialize(*)
    super
    self.errors ||= []
  end
end
查看更多
登录 后发表回答