What does options={} do?

A

anne001

I thought options={} would initialize options as an empty hash.
It does not seem to do so in initialize, but it does outside of it,
why?
!/usr/bin/env ruby

class Cat
def initialize( options={} )
puts "options"
puts options
options={}
puts "options2"
puts options
end
end

Cat.new:)wait=>10 )

Why do I get two different results for options and options2?
options
wait10
options2
 
M

Marcin Mielżyński

anne001 said:
I thought options={} would initialize options as an empty hash.
It does not seem to do so in initialize, but it does outside of it,
why?
!/usr/bin/env ruby

class Cat
def initialize( options={} )
puts "options"
puts options
options={}
puts "options2"
puts options
end
end

Cat.new:)wait=>10 )

Why do I get two different results for options and options2?
options
wait10
options2

....def initialize( options={} )...

defines a method with a default argument (just like in c++) so if you
supply a value when calling it, the value will be used instead of
default one (no surprise). The second output is an empty hash because
you assigned it.

btw: use Kernel::p instead of Kernel::puts for debug purposes.


lopex
 
K

Kenosis

Although I hate the phase "it works for me", well, it works for me. In
your case, in "initialize(options={})", "={}" is a default parameter
value, ie, its only applied to the argument "options" if you DON'T pass
a hash argument to initialize(), which you DO in your example. Try
doing a "Cat.new()" with no argument and you'll see the default empty
hash is used.

Ken
 
A

anne001

I found method argument page 347 of Pickaxe 2.
options is set to the value on the right if initialize is called
without a parameter.

I don't know C++ and I find it counterintuitive that a={} does not mean
a={}

Other than as argument to a method, are there other condition when that
is true?
 
M

Marcin Mielżyński

anne001 said:
I found method argument page 347 of Pickaxe 2.
options is set to the value on the right if initialize is called
without a parameter.

I don't know C++ and I find it counterintuitive that a={} does not mean
a={}

Other than as argument to a method, are there other condition when that
is true?

a={} means that a will be {} _unless_ value is provided for it, so:

def meth arg="default"
puts arg
end

# default value
meth

# provided
meth "provided"

will output

-> defualt
-> provided

lopex
 
A

anne001

Thank you I think you are trying to show me that this fits with ruby,
object, methods...
but I did not get what you are saying yet.

Why not

def initialize( options || {:wait=>20})
...
end
unfortunately, it gives an error message! why?

You would still have to know the rule about ruby and ||, about it
returning the element and not true false. But at least it would do what
it says.
 
M

Marcin Mielżyński

anne001 said:
Thank you I think you are trying to show me that this fits with ruby,
object, methods...
but I did not get what you are saying yet.

Default arguments are quite popular in other languages and ruby
implements them using the same rules. It has nothing to do with objects,
just methods (procedures)
Why not

def initialize( options || {:wait=>20})
..
end
unfortunately, it gives an error message! why?

This construct doesn't make any sense for me. Two things:
1) querying value of non-existent variable 'options'
2) there's no actual parameter defined here

this one make sens o the other side:

def options
nil
end

def method( argument=options || {:wait=>20})
p argument
end

method # invoke it



'options' cannot be local variable since local variables in ruby are
local to methods
You would still have to know the rule about ruby and ||, about it
returning the element and not true false. But at least it would do what
it says.

right, so it will return a hash.

lopex
 
D

Dumaiu

I believe Mr. Mielzynski's point is that
def initialize( options || {:wait=>20})

fails because the parameter list of a function is not subject to
expression execution the way its body is. You can only do a few things
in the list: give parameters some limited type specification with '*'
and '&'; and give them default values, which we are discussing here.
In

def initialize(options = {})

the 'options = {}' statement isn't really an assignment, but a borrowed
C++ idiom providing a pattern for a possible future assignment with a
built-in test. That last example behaves like

def initialize( *options )
options.empty? && options={}

but is safer and, once you get used to it, less confusing. It
originated as an improvement on C allowing for type-safe optional
parameters. Because in C/C++ the number and size (read 'space in
memory') of arguments passed to a function must be known at
compile-time, one way (the other being overloading) to provide
aforesaid flexibility is to provide a placeholder that fills in an
argument the user can leave out. The following C++ function

int f( int x=0, int y=0 )
{ return( x+y ); }

can be invoked as

f(); // 0+0=0

or

f(1); // 1+0=1

or

f(1,1); // 1+1=2

(but not

f(1,1,1); // Compiler error

because no 'f(x,y,z)' was defined).
That the idiom takes the form of what looks like an assignment
statement is due to C++'s avoiding introducing new keywords whenever
possible, a fact that makes for reuse of syntactic structures in some
surprising ways. Interpreted languages such as Ruby have much more
flexible invocation rules, so the C++ idiom is not, strictly speaking,
necessary here. But it is extremely well-known. If you wanted to
write a method in Ruby that took an optional number of arguments, how
would you do it?
 
A

anne001

Interpreted languages such as Ruby have much more flexible invocation
Thank you so much for your responses. It helps me understand arguments,
and its specifics better
the parameter list of a function is not subject to expression execution the way its body is.
a borrowed idiom provides a pattern for a possible future assignment with a built-in test.
'options' cannot be a local variable since local variables in ruby are local to methods

you do have to name an argument, not just initialize it... so I can see
that argument passing may be more concise with its own convention.

thank you.
 

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
474,206
Messages
2,571,074
Members
47,679
Latest member
Justforamoment

Latest Threads

Top