hash = {"_id"=> 12, "result"=>2177, "time" => '2014-05-22 06:15:45 UTC' }
"%s => %s" % hash.values_at('time', 'result')
# => "2014-05-22 06:15:45 UTC => 2177"
# !> too many arguments for format string
hash = {"_id"=> 12, :result => 2177, :time => '2014-05-22 06:15:45 UTC' }
"%{time} => %{result}" % hash # !> possibly useless use of % in void context
# => "2014-05-22 06:15:45 UTC => 2177"
# the below is not working
hash = {"_id"=> 12, "result"=>2177, "time" => '2014-05-22 06:15:45 UTC' }
"%{time} => %{result}" % hash
# ~> -:6:in `%': key{time} not found (KeyError)
# ~> from -:6:in `<main>'
Does it mean I can use String#% to hash only when Hash will have keys as a
symbol ? Or there is a trick, that I am missing.
# the below is not working
hash = {"_id"=> 12, "result"=>2177, "time" => '2014-05-22 06:15:45 UTC' }
"%{time} => %{result}" % hash
# ~> -:6:in `%': key{time} not found (KeyError)
# ~> from -:6:in `<main>'
Does it mean I can use String#% to hash only when Hash will have keys as a
symbol ? Or there is a trick, that I am missing.
Format---Uses str as a format specification, and returns the result of
applying it to arg. If the format specification contains more than one
substitution, then arg must be an Array or Hash containing the values to
be substituted. See Kernel::sprintf for details of the format string.
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.
Format---Uses str as a format specification, and returns the result of
applying it to arg. If the format specification contains more than one
substitution, then arg must be an Array or Hash containing the values to
be substituted. See Kernel::sprintf for details of the format string.
What is not true any more? You are just confirming what I wrote above.
I read your statement to say that String#% doesn't handle hashes, tho on second reading you might have meant that it doesn't #hash. Not that I can figure out why you'd say such a thing. Arup's mail was pretty clear that he was using String#% with a hash argument.
Format---Uses str as a format specification, and returns the result of
applying it to arg. If the format specification contains more than one
substitution, then arg must be an Array or Hash containing the values to
be substituted. See Kernel::sprintf for details of the format string.
What is not true any more? You are just confirming what I wrote above.
I read your statement to say that String#% doesn't handle hashes, tho on second reading you might have meant that it doesn't #hash. Not that I can figure out why you'd say such a thing. Arup's mail was pretty clear that he was using String#% with a hash argument.
Well, I read it differently but there you go.
Not exactly your point. None of your examples of sprintf are like the last example of the String#% rdoc:
"foo = %{foo}" % { :foo => 'bar' }
returns:
"foo = bar"
So you were confusing the issue when the OP was clearly using a hash argument.
I was replying to the OP's question "Does it mean I can use String#%
to hash?" You cannot use String#% to hash something - it's just a
formatting function. I just did not provide examples for all use
cases.
Cheers
robert
···
On Mon, Jun 2, 2014 at 12:20 PM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
On Jun 2, 2014, at 0:27, Robert Klemme <shortcutter@googlemail.com> wrote:
On Mon, Jun 2, 2014 at 6:55 AM, Ryan Davis <ryand-ruby@zenspider.com> wrote:
I suspect Arup meant "with a hash" rather than "to hash." Only folk with
a certain amount of computer science type background would consider "to
hash" to be a verb. Further, I'd imagine someone without that background,
and maybe without the same fluency in English, would have misinterpreted
your "String#% does not hash" statement similarly (i.e. as "String#% does
not accept a hash"), and thus confusion ensues.
Though it'd be kind of cool if String#% had a format thingy for the MD5 of
the input.
···
On 2 June 2014 21:30, Robert Klemme <shortcutter@googlemail.com> wrote:
I was replying to the OP's question "Does it mean I can use String#%
to hash?" You cannot use String#% to hash something - it's just a
formatting function. I just did not provide examples for all use
cases.
That's my mistake and bad English made you guys confused.
My straight question was -
We can pass a hash object as an argument to the method *String#%*. But when
the hash will be having keys as a string object, if i try to access it, it
will throw error as below :
hash = { 'apple' => 2 }
"I want %{apple} apples" % hash #key{apple} not found (KeyError)
But no problem, while the hash is having keys as symbols -
hash = { :apple => 2 }
"I want %{apple} apples" % hash
# => "I want 2 apples"
Why so? Because I didn't find any such *note* in the documentation.
···
On Monday, June 02, 2014 01:30:51 PM Robert Klemme wrote:
> So you were confusing the issue when the OP was clearly using a hash
> argument.
I was replying to the OP's question "Does it mean I can use String#%
to hash?" You cannot use String#% to hash something - it's just a
formatting function. I just did not provide examples for all use
cases.
--
Regards,
Arup Rakshit
Debugging is twice as hard as writing the code in the first place. Therefore,
if you write the code as cleverly as possible, you are, by definition, not
smart enough to debug it.