You want to make variables as local as possible.  Then, you want to make 
things constants whenever possible.  When I write straight scripts I 
tend to always make everything a Constant (begins with uppercase) until 
I find something that needs otherwise, which turns out to be more rare 
than most people think.


On 11/05/2013 08:10 AM, Stu P. D'naim wrote:
> Every tutorial I read says global variables are bad, but I'm struggling
> with the ways to avoid them. In this case (wich actually works), I have
> 3 files - each having it's own class and purpose. Class Menu is where
> program begins. Class Mynumbers should generate a bunch of variables
> through calculations. Class Myfiles should be able to read/write those
> variables ... very simplified version of my program looks like this:
>
> -------------------------------------
> #menu.rb
>
> require_relative "myfiles.rb"
> require_relative "mynumbers.rb"
>
> class Menu
>    def initialize
>      $nums = Mynumbers.new
>      $nums.calculate
>      Myfiles.show
>    end
> end
>
> start = Menu.new
> -------------------------------------
> #mynumbers.rb
>
> class Mynumbers
>    attr_reader :my_var1, :my_var2
>
>    def calculate
>      @my_var1 = rand(2..31)
>      @my_var2 = rand(200..310)
>    end
> end
> ------------------------------------
> #myfiles.rb
>
> class Myfiles
>    def self.show
>      puts $nums.class
>      puts $nums.my_var1
>      puts $nums.my_var2.inspect
>    end
> end
> -------------------------------------
>
> TL;DR - is there a way to avoid using global instance $nums ?
> (and is there really a reason for that, since my program works)
>