# Confuse about initialize hash

**URL:** <https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289>\
**Category:** Ruby\
**Created:** [November 21, 2009, 7:27am UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289 "2009-11-21T07:27:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 21, 2009, 7:27am UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/1 "2009-11-21T07:27:21Z")

</div>

def in(options ={} )  
p options  
end

in(:say =\> “somehting”) # {:say =\> “something”}

## but under the hood of it, doesn’t that process equivalent to

## options = {} options = :say =\> “something”

## or

## options = :say =\> “something”

??? none of this can compiler.  
silly question, hope someone help.

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 21, 2009, 10:12am UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/2 "2009-11-21T10:12:37Z")

</div>

Zhenning G. wrote:

> def in(options ={} )  
> p options  
> end
> 
> in(:say =\> “somehting”) # {:say =\> “something”}
> 
> ## but under the hood of it, doesn’t that process equivalent to
> 
> ## options = {} options = :say =\> “something”
> 
> ## or
> 
> ## options = :say =\> “something”
> 
> ??? none of this can compiler.  
> silly question, hope someone help.

Hey Zhenning G.,

Hope the following sample will help you,

def in(options)  
p options.class  
end  
in(:say =\> ‘some\_value’)

the type of argument passed to any method resembles inside the method,  
if you send hash it shows the class as ‘Hash’. No need to initialize  
hash or anything in ruby script. It will assign automatically according  
to the values.

thanks,  
vaddi

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 21, 2009, 1:16pm UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/3 "2009-11-21T13:16:53Z")

</div>

Hi –

On Sat, 21 Nov 2009, Zhenning G. wrote:

> * * *
> 
> ## or
> 
> ## options = :say =\> “something”
> 
> ??? none of this can compiler.

Don’t name your method ‘in’. That’s a reserved word.

David

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 21, 2009, 1:18pm UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/4 "2009-11-21T13:18:03Z")

</div>

Hi –

On Sat, 21 Nov 2009, vadivelan .K wrote:

> > options = :say =\> “something”  
> > Hope the following sample will help you,
> 
> def in(options)  
> p options.class  
> end  
> in(:say =\> ‘some\_value’)
> 
> the type of argument passed to any method resembles inside the method,  
> if you send hash it shows the class as ‘Hash’. No need to initialize  
> hash or anything in ruby script. It will assign automatically according  
> to the values.

Have you tried actually running your example? 🙂

David

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 21, 2009, 1:54pm UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/5 "2009-11-21T13:54:21Z")

</div>

David A. Black wrote:

> Have you tried actually running your example? 🙂
> 
> David

Sorry for the method name.  
how about replace in with “example”, just a method name here.  
all I confusing is the process. the hash parameter is used a lot in the  
rails code.

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 21, 2009, 3:00pm UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/6 "2009-11-21T15:00:33Z")

</div>

Zhenning G. wrote:

> all I confusing is the process. the hash parameter is used a lot in the  
> rails code.

It is a special case: when the last argument to a method call is a hash,  
then you can omit the outer braces.

> > def example(\*args); p args; end  
> > =\> nil
> 
> > example(123, 456, {“foo”=\>9, “bar”=\>11})  
> > [123, 456, {“foo”=\>9, “bar”=\>11}]  
> > =\> nil
> 
> > example(123, 456, “foo”=\>9, “bar”=\>11)  
> > [123, 456, {“foo”=\>9, “bar”=\>11}]  
> > =\> nil
> 
> >

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 22, 2009, 10:56pm UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/7 "2009-11-22T22:56:54Z")

</div>

Zhenning G. wrote:

> Brian C. wrote:
> 
> > It is a special case: when the last argument to a method call is a hash,  
> > then you can omit the outer braces.
> 
> the hash options I saw in atom\_feed , but it doesn’t the last argument.

Is it the &block that bothers you?

That’s another special bit of syntax. It allows you to capture the  
passed block into a variable. For example,

def foo  
yield 123  
end

is pretty much the same as

def foo(&blk)  
blk.call(123)  
end

But in the second case, since the block itself is bound to a variable,  
you can in turn pass the block around to other methods.

You might call foo like this:

foo { |x| puts “The value is #{x}” }  
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  
This is the ‘block’ I’m talking about

So the block is a hidden argument which comes after all the other  
arguments, and unless you ask for it with &, it’s entirely hidden. So  
the last _real_ argument may be a hash without its outer braces. I hope  
that’s a bit clearer 🙂

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [December 1, 2009, 2:43am UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/8 "2009-12-01T02:43:20Z")

</div>

Brian C. wrote:

> Zhenning G. wrote:
> 
> > Brian C. wrote:
> > 
> > > It is a special case: when the last argument to a method call is a hash,  
> > > then you can omit the outer braces.  
> > > But in the second case, since the block itself is bound to a variable,  
> > > you can in turn pass the block around to other methods.

> So the block is a hidden argument which comes after all the other  
> arguments, and unless you ask for it with &, it’s entirely hidden. So  
> the last _real_ argument may be a hash without its outer braces. I hope  
> that’s a bit clearer 🙂

–  
I know block, but don’t know about the hash parameter

---

<div class="post-metadata">

**Author:** ![rubyernewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9dc877/32.png) [@rubyernewbie](https://www.ruby-forum.com/u/rubyernewbie)\
**Post date:** [November 22, 2009, 2:20pm UTC](https://www.ruby-forum.com/t/confuse-about-initialize-hash/179289/9 "2009-11-22T14:20:00Z")

</div>

Brian C. wrote:

> It is a special case: when the last argument to a method call is a hash,  
> then you can omit the outer braces.

the hash options I saw in atom\_feed , but it doesn’t the last argument.

==============================================================  
def atom\_feed(options = {}, &block)  
95: if options[:schema\_date]  
96: options[:schema\_date] =  
options[:schema\_date].strftime(“%Y-%m-%d”) if  
options[:schema\_date].respond\_to?(:strftime)  
97: else  
98: options[:schema\_date] = “2005” # The Atom spec copyright  
date  
99: end  
100:  
101: xml = options[:xml] || eval(“xml”, block.binding)  
102: xml.instruct!  
103: if options[:instruct]  
104: options[:instruct].each do |target,attrs|  
105: if attrs.respond\_to?(:keys)  
106: xml.instruct!(target, attrs)  
107: elsif attrs.respond\_to?(:each)  
108: attrs.each { |attr\_group| xml.instruct!(target,  
attr\_group) }  
109: end  
110: end  
111: end  
112:  
113: feed\_opts = {“xml:lang” =\> options[:language] || “en-US”,  
“xmlns” =\> ‘[Atom Syndication Format namespace](http://www.w3.org/2005/Atom)’}  
114: feed\_opts.merge!(options).reject!{|k,v|  
!k.to\_s.match(/^xml/)}  
115:  
116: xml.feed(feed\_opts) do  
117: xml.id(options[:id] ||  
“tag:#{request.host},#{options[:schema\_date]}:#{request.request\_uri.split(”.“)[0]}”)  
118: xml.link(:rel =\> ‘alternate’, :type =\> ‘text/html’, :href  
=\> options[:root\_url] || (request.protocol + request.host\_with\_port))  
119: xml.link(:rel =\> ‘self’, :type =\> ‘application/atom+xml’,  
:href =\> options[:url] || request.url)  
120:  
121: yield AtomFeedBuilder.new(xml, self, options)  
122: end  
123: end
