0

I Have a struct - let's say it looks like this:

 class MyClass::Subclass < Struct.new(:model1, :model2)
   def method1
   end

   def method2
     if model1.active?

     end
   end
 end

This is how I currently have a subclass setup. I am now at a point where I have to pass in a one-time options.

My thinking is that there must be something similar to what you can do in methods like:

class MyClass::Subclass < Struct.new(:model1, :model2, options = {})
  def method1
    if options["need_this"]

    end
  end
end

I keep on getting errors:

TypeError: {} is not a symbol

Is there something like options = {} that I can use in Structs? Sorry this may seem like a newb question but Ruby is not my main language.

luke
  • 1,513
  • 2
  • 20
  • 38
  • Possible duplicate: https://stackoverflow.com/questions/15055852/how-to-change-the-default-value-of-a-struct-attribute – spickermann Sep 20 '19 at 18:50
  • 1
    It would be helpful if you could explain, what *precisely* is unclear to you in the documentation of `Struct::new`. That way, the Ruby developers can improve the documentation so that future Ruby developers don't stumble over the same problems you did. Help make the world a better place! (It would also help better focussing the answers to your question if we know ahead of time what you *do* and *don't* understand in the documentation.) – Jörg W Mittag Sep 20 '19 at 19:27
  • There was nothing unclear. I guess I was kind of looking for guidance in how I should structure something that requires an `options` parameter. My brain was fried when I posted so I wasn't thinking of turning the `options` into a symbol. Again Ruby isn't my main language so apologize if this was a poor question. – luke Sep 23 '19 at 15:53

1 Answers1

1

I'm getting that what you are trying to do is add an optional list of options. (Please correct me if I'm wrong.) To do that, simply add the element. Then in your code, check to see whether or not a hash has been provided:

class MyClass::Subclass < Struct.new(:model1, :model2, :options)
  def method1
    if self.options && self.options["need_this"]
      p 'need this'
    end
  end
end

x = MyClass::Subclass.new(:x, :y, {'need_this' => 'yesido'})
x.method1 # => 'need this'

You specify Struct parameters as symbols (hence your error), and you can pass any type of arguments you want to them — including none at all. So, while you can't specify a default value, you can check whether a value has been provided; if not, in this case, options will be nil. The example I've put in checks whether a hash has been provided for options, and if so, whether a need_this option has been included in the hash.

Here's the ruby doc on Struct. If you read the overview and the doc on the new method, you should have a clear picture of the Struct syntax.

BobRodes
  • 5,990
  • 2
  • 24
  • 26
  • Thanks Bob - I ended up playing around this weekend and figuring this out. This is exactly how I am using it. Thanks for the clear answer and code :) – luke Sep 23 '19 at 15:52