Options in a class

P

Paul Bergstrom

I have some problems understanding the basics of classes and modules in
Ruby =E2=80=93=C2=A0and arg and options. How do you create a e g class th=
at 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 =3D> 'red') or Abc.test()

-- =

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

Andrew Wagner

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

puts Abc.test # =3D> red
puts Abc.test:)blue) #=3D> blue
 
I

Intransition

I have some problems understanding the basics of classes and modules in
Ruby =96=A0and arg and options. How do you create a e g class that can ta= ke
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 =3D> 'red') or Abc.test()

Simply:

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

But I usually do:

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

Also, if you want args and opts,

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

Jesús Gabriel y Galán

Try this:
module Abc
=A0def self.test(arg =3D> :red)
=A0 =A0puts arg
=A0end
end

puts Abc.test # =3D> red
puts Abc.test:)blue) #=3D> blue

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

irb(main):010:0> module Abc
irb(main):011:1> def self.test(color =3D :red)
irb(main):012:2> puts color
irb(main):013:2> end
irb(main):014:1> end
=3D> nil
irb(main):015:0> Abc.test
red
=3D> nil
irb(main):016:0> Abc.test:)blue)
blue
=3D> 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 =3D {})
irb(main):019:2> color =3D options[:color] || :red
irb(main):020:2> size =3D options[:size] || 3
irb(main):021:2> puts color,size
irb(main):022:2> end
irb(main):023:1> end
=3D> nil
irb(main):024:0> Abc.test
red
3
=3D> nil
irb(main):025:0> Abc.test:)color =3D> :blue)
blue
3
=3D> nil
irb(main):027:0> Abc.test:)color =3D> :blue, :size =3D> 5)
blue
5
=3D> nil

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

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

or like this:

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

Jesus.
 
J

Jeremy Bopp

Also, if you want args and opts,

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

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

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


-Jeremy
 
P

Paul Bergstrom

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?
 
J

Jesús Gabriel y Galán

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?

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.
 
P

Paul Bergstrom

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 :)
 
J

Jeremy Bopp

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 = {})
puts opts[:color]
end
end

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

Paul Bergstrom

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

Jeremy Bopp

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top