[prev in list] [next in list] [prev in thread] [next in thread] 

List:       ruby-talk
Subject:    Re: Module using object defined outside of method?
From:       Leam Hall <leamhall () gmail ! com>
Date:       2018-02-14 11:52:49
Message-ID: 7db62acd-2f7f-7e47-b470-51a0b114b237 () gmail ! com
[Download RAW message or body]

On 02/13/2018 10:00 AM, Steve Turczyn wrote:
> If you don't actually need to store any state information (e.g. "what was the last \
> roll of that die?") then you're wasting your time creating a class where a module \
> will work fine. 
> module Dice
> def self.roll_1
> rand(1..6)
> end
> end
> 
> Then just reference it where you need it with...
> 
> Dice.roll_1
> 
> If you really want to use a class, you can stick to your original code but create a \
> disposable instance... something that only exists for the moment it's needed... 
> Dice.new.roll_1
> 
> ...but this is only really useful when the "roll_1" method needs to reference other \
> methods and you therefore need to store some state information to complete the \
> task. 
> So (again) you only need a class object if you need to keep track of states/values.

Steve, the roll methods are currently in a module for a specific task. I 
am moving them to a general task class/module and wanted to play with 
classes more. The rolls don't need to maintain state, but there are 
other rolls that call the base.

This is mostly me figuring out OOP as I stumble along.  :)

Thanks!

Leam


Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic