Options in a class

I have some problems understanding the basics of classes and modules in
Ruby – and arg and options. How do you create a e g class that can take
some options, but leave them out if not needed? This is what I'm
familiar with now, sending along an arg to the method in a module.

module Abc
def self.test(arg)
puts arg
end
end

In this occasion I have to pass along a an arg, or nil, or there will an
error. But how do I design my module so I can pass options, or leave
them out if wanted? Like:

Abc.test(:color => 'red') or Abc.test()

···

--
Posted via http://www.ruby-forum.com/.

Try this:
module Abc
  def self.test(arg => :red)
    puts arg
  end
end

puts Abc.test # => red
puts Abc.test(:blue) #=> blue

···

On Wed, Oct 13, 2010 at 9:41 AM, Paul Bergstrom <pal@palbergstrom.com>wrote:

I have some problems understanding the basics of classes and modules in
Ruby – and arg and options. How do you create a e g class that can take
some options, but leave them out if not needed? This is what I'm
familiar with now, sending along an arg to the method in a module.

module Abc
def self.test(arg)
puts arg
end
end

In this occasion I have to pass along a an arg, or nil, or there will an
error. But how do I design my module so I can pass options, or leave
them out if wanted? Like:

Abc.test(:color => 'red') or Abc.test()

--
Posted via http://www.ruby-forum.com/\.

Simply:

  class Abc
    def initialize(opts={})
      @color = opts[:color]
    end
  end

But I usually do:

  class Abc
    attr_accessor :color
    def initialize(opts={})
      opts.each do |k,v|
        send("#{k}=", v)
      end
    end
  end

Also, if you want args and opts,

    def initialize(*args)
      opts = (Hash === args.last ? args.pop : {})
      ...

···

On Oct 13, 9:41 am, Paul Bergstrom <p...@palbergstrom.com> wrote:

I have some problems understanding the basics of classes and modules in
Ruby – and arg and options. How do you create a e g class that can take
some options, but leave them out if not needed? This is what I'm
familiar with now, sending along an arg to the method in a module.

module Abc
def self.test(arg)
puts arg
end
end

In this occasion I have to pass along a an arg, or nil, or there will an
error. But how do I design my module so I can pass options, or leave
them out if wanted? Like:

Abc.test(:color => 'red') or Abc.test()

What about being able to pass different values to :color, like :color =>
'red or :color => 'blue'? How do I deal with that inside the module?

···

--
Posted via http://www.ruby-forum.com/.

What if I would like to pass several values, with some set with a
default value if not declared?

···

--
Posted via http://www.ruby-forum.com/.

The above should be '=', not '=>'. Like this:

irb(main):010:0> module Abc
irb(main):011:1> def self.test(color = :red)
irb(main):012:2> puts color
irb(main):013:2> end
irb(main):014:1> end
=> nil
irb(main):015:0> Abc.test
red
=> nil
irb(main):016:0> Abc.test(:blue)
blue
=> nil

As you mentioned "options", it seems that you might want to allow
passing several options, not just one argument. The usual idiom for
that is using a hash:

irb(main):017:0> module Abc
irb(main):018:1> def self.test(options = {})
irb(main):019:2> color = options[:color] || :red
irb(main):020:2> size = options[:size] || 3
irb(main):021:2> puts color,size
irb(main):022:2> end
irb(main):023:1> end
=> nil
irb(main):024:0> Abc.test
red
3
=> nil
irb(main):025:0> Abc.test(:color => :blue)
blue
3
=> nil
irb(main):027:0> Abc.test(:color => :blue, :size => 5)
blue
5
=> nil

You could also have the default values in the default argument:

def self.test(options = {:color => :red, :size => 3})

or like this:

def self.test(options = {})
options = {:color => :red, :size => 3}.merge(options)
end

Jesus.

···

On Wed, Oct 13, 2010 at 3:46 PM, Andrew Wagner <wagner.andrew@gmail.com> wrote:

Try this:
module Abc
def self.test(arg => :red)
puts arg
end
end

puts Abc.test # => red
puts Abc.test(:blue) #=> blue

And if you want a specific list of arguments followed by options:

def initialize(arg1, arg2, opts = {})
  ...
end

-Jeremy

···

On 10/13/2010 8:59 AM, Intransition wrote:

Also, if you want args and opts,

    def initialize(*args)
      opts = (Hash === args.last ? args.pop : {})
      ...

You want to pass several colors in one go? Use an array as the value:

irb(main):028:0> module Abc
irb(main):029:1> def self.test(options = {})
irb(main):030:2> colors = options[:colors] || [:red]
irb(main):031:2> puts "The chosen colors are: #{colors.inspect}"
irb(main):032:2> end
irb(main):033:1> end
=> nil
irb(main):034:0> Abc.test(:colors => [:red, :green, :blue])
The chosen colors are: [:red, :green, :blue]

Jesus.

···

On Wed, Oct 13, 2010 at 4:16 PM, Paul Bergstrom <pal@palbergstrom.com> wrote:

What about being able to pass different values to :color, like :color =>
'red or :color => 'blue'? How do I deal with that inside the module?

Thomas Sawyer wrote in post #949846:

Simply:

  class Abc
    def initialize(opts={})
      @color = opts[:color]
    end
  end

But I usually do:

  class Abc
    attr_accessor :color
    def initialize(opts={})
      opts.each do |k,v|
        send("#{k}=", v)
      end
    end
  end

Also, if you want args and opts,

    def initialize(*args)

There it is. Thanks :slight_smile:

···

      opts = (Hash === args.last ? args.pop : {})
      ...

--
Posted via http://www.ruby-forum.com/\.

module Abc
  def self.test(opts = {})
    puts opts[:color]
  end
end

Abc.test(:color => 'red') # => red
Abc.test(:color => 'blue') # => blue

···

On 10/13/2010 9:16 AM, Paul Bergstrom wrote:

What about being able to pass different values to :color, like :color =>
'red or :color => 'blue'? How do I deal with that inside the module?

module Abc
  def self.test(opts = {})
    default_opts = {
      :color => 'yellow',
      :size => 'small'
    }
    opts = default_opts.merge(opts)

    puts "color = #{opts[:color]}, size = #{opts[:size]}"
  end
end

Abc.test
# => color = yellow, size = small
Abc.test(:color => 'red')
# => color = red, size = small
Abc.test(:size => 'large')
# => color = yellow, size = large
Abc.test(:color => 'blue', :size => 'humongous')
# => color = blue, size = humongous

-Jeremy

···

On 10/15/2010 9:18 AM, Paul Bergstrom wrote:

What if I would like to pass several values, with some set with a
default value if not declared?