Home > Blockchain >  Best convention for using ARGV with class in Ruby
Best convention for using ARGV with class in Ruby

Time:11-20

  • situation

I'm learning Ruby's class and constant now.

I thought example code for my class, and decide to make class for practice.

I will use ARGV for argument of my script.

But i'm wondering for right convention for using ARGV with class.

My question is,

  1. Where should i place my code which access to ARGV? I thought several example for structure. (A and B) Which one is better for convention?

  2. Is this right convention for using ARGV inside of class like example A?

  • A
class ExampleClass
  CONSTANT = ARGV[0]

  def example_function
    puts CONSTANT
  end
end

ex = ExampleClass.new
ex.example_function
  • B
class ExampleClass
  def example_function
    puts CONSTANT
  end
end

CONSTANT = ARGV[0]
ex = ExampleClass.new
ex.example_function

CodePudding user response:

I would do:

class ExampleClass
  def initialize(args)
    @args = args
  end

  def example_function
    @args
  end
end

ex = ExampleClass.new(ARGV[0])
ex.example_function
  •  Tags:  
  • ruby
  • Related