Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A couple little tips...</p> <p>[1]</p> <p>A slightly more idiomatic way to write this...</p> <pre><code>for i in (1..36) lots[i] = Lot.new(i) end </code></pre> <p>would be...</p> <pre><code>(1..36).each { |i| lots[i] &lt;&lt; Lot.new(i) } </code></pre> <p>[2]</p> <p>To remove a Guest from a Lot, you might want to set it to <code>nil</code> rather than <code>false</code>. This would be my suggestion...</p> <pre><code>class Lot def initialize(number) @gauge = rand(2000) + 2000 @number = number # Don't need to set @guest -- it's nil by default. end # In Ruby, methods that return a boolean often have a "?". # Makes it "read better" when you call the method. (See # usage sample.) def occupied? ! @guest.nil? end # There's a more commonplace way to do this. See below... def add_guest(guest) @guest = guest end def remove_guest() @guest = nil end end </code></pre> <p>Example of usage:</p> <pre><code>&gt;&gt; lot = Lot.new(2) =&gt; #&lt;Lot:0x1300920 @number=2, @gauge=3444&gt; &gt;&gt; lot.occupied =&gt; false &gt;&gt; lot.add_guest('A guest') =&gt; "A guest" &gt;&gt; lot.occupied? =&gt; true &gt;&gt; lot.remove_guest =&gt; nil &gt;&gt; lot.occupied? =&gt; false </code></pre> <p>Take two...</p> <p>It's conventional to use <code>attr_accessor</code> methods in your class definition. They automatically add getter and setter methods to your class. You could do that instead of <code>add_guest</code> and <code>remove_guest</code> if you wanted to follow the common Ruby pattern...</p> <pre><code>class Lot attr_accessor :number, :gauge, :guest def initialize(number) @gauge = rand(2000) + 2000 @number = number end def occupied? ! @guest.nil? end end </code></pre> <p>Usage...</p> <pre><code>irb(main):017:0&gt; lot = Lot.new(3) =&gt; #&lt;Lot:0xb7f7fca8 @gauge=3186, @number=3&gt; </code></pre> <p>Set the Guest of a Lot (like <code>add_guest</code>)...</p> <pre><code>irb(main):019:0&gt; lot.guest = 'A guest' =&gt; "A guest" irb(main):020:0&gt; lot.occupied? =&gt; true </code></pre> <p>Get the Guest for a Lot...</p> <pre><code>irb(main):025:0&gt; lot.guest =&gt; "A guest" </code></pre> <p>Remove the Guest...</p> <pre><code>irb(main):021:0&gt; lot.guest = nil =&gt; nil irb(main):023:0&gt; lot.occupied? =&gt; false </code></pre>
 

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