-9

How do I automatically generate setters and getters for a node class in ruby? I am using eclipse

class Node

  def initialize(*args)

    if args.size > 2 or args.size < 1
        raise "initializer needs 0,1 or 2 arguments"    
    elseif args.size == 1
      @data = args[0] 
    else
      @data = args[0]
      @next = args[1]   
    end
  end

  def to_s

  end
end
sjagr
  • 15,983
  • 5
  • 40
  • 67
nnxcb g
  • 11
  • 1
  • Hello guys, how do I automatically generate setters and getters for a node class in ruby? I am using eclipse – nnxcb g Mar 30 '15 at 18:02
  • 4
    Yikes! -8 in just 17 minutes. Call Guinness! Moral: never, ever, post a question and walk away. Also, to clarify, edit the question rather than try to explain in comments. – Cary Swoveland Mar 30 '15 at 18:20

1 Answers1

1

The idiomatic Ruby approach would be attr_accessor :data, :next (also see "What is an accessor?).

Then when, and only when, custom behavior was required would an overload / explicit method implementation be added. Ruby is not Java; don't need to add boilerplate code to everything.

If desiring to "see the code anyway" then a custom template can be added to Eclipse. The attr_accessor links above explain how such a template ought to look to mimic the attr_* behaviors.

Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220