confuse about initialize hash

Z

Zhenning Guan

def in(options ={} )
p options
end

in:)say => "somehting") # {:say => "something"}

but under the hood of it, doesn't that process equivalent to
 
V

vadivelan .K

Zhenning said:
def in(options ={} )
p options
end

in:)say => "somehting") # {:say => "something"}

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

Hey Zhenning Guan,

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
 
D

David A. Black

Hi --

def in(options ={} )
p options
end

in:)say => "somehting") # {:say => "something"}

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

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


David
 
D

David A. Black

Hi --

Hey Zhenning Guan,

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
 
Z

Zhenning Guan

David said:
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.
 
B

Brian Candler

Zhenning said:
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.
[123, 456, {"foo"=>9, "bar"=>11}]
=> nil[123, 456, {"foo"=>9, "bar"=>11}]
=> nil
 
Z

Zhenning Guan

Brian said:
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" => '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
 
B

Brian Candler

Zhenning said:
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 :)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top