0

I wonder how I can execute and test dependent files in ruby. Let's take those simple files as instance:

thing.rb

class Thing
    def initialize(name, description)
        @name = name
        @description = description
    end

    def set_name(name)
        @name = name
    end

    def get_name
        return @name
    end

    def set_description(description)
        @description = description
    end

    def get_description
        return @description
    end
end

treasure.rb

class Treasure < Thing
    def initialize(name, description, value)
        super(name, description)
        @value = value                                  
    end

    def set_value(value)
        @value = value
    end

    def get_value
        return @value
    end
end

As I have a Treasure class which is a descendant of Thing class, there's a dependency explicit here. Wether I create object by Treasure class, is need which the Thing class has received values at her instances variables of each method. So, how is possible to execute this ruby files by prompt (Windows) or by terminal (GNU/Linux) for test them without make use of IDE?

Leandro Arruda
  • 466
  • 6
  • 16
  • Here is how I interpret your question: "how can I create an instance of the Treasure class in IRB without getting a LoadError or NameError?" Is that an accurate summary? – Travis Hohl Sep 29 '14 at 20:58
  • On really I want to know how I can execute this files on terminal or prompt. I execute a single file using ruby , so how can I do the same with dependent files? Which parameters can I use? – Leandro Arruda Sep 29 '14 at 21:02
  • are you trying to generate a new gem? – Малъ Скрылевъ Sep 29 '14 at 21:28
  • 1
    @LeandroArruda You didn't ask about this, but you should know that you can automatically create attribute getters and setters using `attr_accessor`. See: http://stackoverflow.com/questions/4370960/what-is-attr-accessor-in-ruby – Travis Hohl Sep 29 '14 at 21:34
  • @thohl Thank you so much for this great inducation, but my intention was at the first time know how I could to pass values, executing by terminal, to class that depending of others. I did made a mess, but I wanted explain the better way. – Leandro Arruda Sep 29 '14 at 22:10

1 Answers1

2

The ruby command takes a -r argument that lets you file files or gems to require:

ruby -r./thing treasure.rb

Of course, if Treasure always needs Thing (which it does), then you should just require it at the top of the file. If you're using Ruby 1.9.3+, just put this at the top of treasure.rb:

require_relative "thing"

Or, for older versions of Ruby:

require File.expand_path("thing", File.dirname(__FILE__))
Jordan Running
  • 102,619
  • 17
  • 182
  • 182