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.
--
Posted via http://www.ruby-forum.com/.
Zhenning Guan 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 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
···
--
Posted via http://www.ruby-forum.com/\.
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.
···
--
Posted via http://www.ruby-forum.com/\.
Zhenning Guan 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
···
--
Posted via http://www.ruby-forum.com/\.
Brian Candler 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" => '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
--
Posted via http://www.ruby-forum.com/\.
Zhenning Guan wrote:
Brian Candler 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
···
--
Posted via http://www.ruby-forum.com/\.