Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could do something like the following, though I expect there are gems that do this and more, and do it better:</p> <pre><code>module ArgCheck def type_check(label, arg, klass) raise_arg_err label + \ " (= #{arg}) is a #{arg.class} object, but should be be a #{klass} object" unless arg.is_a? klass end def range_check(label, val, min, max) raise_arg_err label + " (= #{val}) must be between #{min} and #{max}" unless val &gt;= min &amp;&amp; val &lt;= max end def min_check(label, val, min) puts "val = #{val}, min = #{min}" raise_arg_err label + " (= #{val}) must be &gt;= #{min}" unless val &gt;= min end def max_check(val, min) raise_arg_err label + " (= #{val}) must be &lt;= #{max}" unless val &lt;= max end # Possibly other checks here private def raise_arg_err(msg) raise ArgumentError, msg + "\n backtrace: #{caller_locations}" end end class Product include ArgCheck attr_accessor :quantity, :type, :price, :imported def initialize(quantity, type, price) # Check arguments min_check 'quantity', quantity, 0 type_check 'type', type, String type_check 'price', price, Float @quantity = quantity @type = type @price = price.round(2) end end product = Product.new(-1, :cat, 3) # =&gt; arg_check.rb:23:in `raise_arg_err': quantity (= -1) must be &gt;= 0 (ArgumentError) # backtrace: ["arg_check.rb:11:in `min_check'", "arg_check.rb:33:in `initialize'", \ # "arg_check.rb:43:in `new'", "arg_check.rb:43:in `&lt;main&gt;'"] product = Product.new(1, :cat, 3) # =&gt; arg_check.rb:26:in `raise_arg_err': type (= cat) is a Symbol object, \ # but should be be a String object (ArgumentError) # backtrace: ["arg_check.rb:3:in `type_check'", "arg_check.rb:34:in `initialize'", \ # "arg_check.rb:48:in `new'", "arg_check.rb:48:in `&lt;main&gt;'"] product = Product.new(1, "cat", 3) # =&gt; arg_check.rb:23:in `raise_arg_err': price (= 3) must be a Float object (ArgumentError) # backtrace: ["arg_check.rb:3:in `type_check'", "arg_check.rb:35:in `initialize'", \ # "arg_check.rb:53:in `new'", "arg_check.rb:53:in `&lt;main&gt;'"] product = Product.new(1, "cat", 3.00) # No exception raised </code></pre> <p>Note that, when run in irb, <a href="http://www.ruby-doc.org/core-2.0.0/Kernel.html#method-i-caller_locations" rel="nofollow"><code>Kernel#caller_locations</code></a> brings in a lot of stuff you don't want, that you won't get when run from the command line.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload