# Simple question regarding hashes

Is something like this possible?

asdf = {“letters” => |||||some code that returns something|||||}

This is difficult for me to explain being that im not a “programmer”
but… here’s what im trying to do:

asdf = {“letters” => (if 1+1 == 3; return “yes”;else return “no”;end)}

### in hopes that this would set asfd[‘letters’] equal to “no”

or… something like this:
asdf = {“letters” => [“a”, “b”, “c”].each {|letter| yield letter if
letter != “b”}}

### in hopes that asdf[‘letters’] will equal [“a”, “b”]

i realize, I could do [“a”, “b”, “c”].delete(“b”) but my question is
can you define the value of a hash based on an iteration, or an if
statement etc…

Thank you

x1 wrote:

or… something like this:
Thank you
Do you want to use the calculation only once, i.e. on insertion? Then

hash = {“foo” => (1+1 == 3 ? “yes” : “no”)}
hash = {“foo” => if 1+1 == 3 then “yes” else “no” end}

``robert``

fr x1:

# statement etc…

if it’s code that you want, then Proc can do anything

irb(main):003:0> hash = {“foo” =>Proc.new {1+1 == 3 ? “yes” : “no”}}
=> {“foo”=>#Proc:0x02b0f124@:3(irb)}
irb(main):005:0> hash[“foo”]
=> #Proc:0x02b0f124@:3(irb)
irb(main):006:0> hash[“foo”].call
=> “no”

is that what you want?
kind regards -botp

x1 wrote:

``````}.call
``````

}
puts hash[‘foo’][“y”][1]
puts hash[‘foo’][“x”][1]

ruby test1.rb
y
b
Exit code: 0

This code looks completely useless to me. You end up with the same hash
whatever you do since there is no parametrization to your code. Where
is the point? Did I miss something?

Kind regards

``robert``

Yep… Thanks alot. This some pretty neat stuff.

hash = {
“foo” => Proc.new {
x = []
y = []
hash = {
“x” => [“a”, “b”, “c”].each {|i| x << i},
“y” => [“x”, “y”, “z”].each {|i| y << i}
}
}.call
}
puts hash[‘foo’][“y”][1]
puts hash[‘foo’][“x”][1]

ruby test1.rb
y
b
Exit code: 0

On Sun, Aug 13, 2006 at 03:54:42AM +0900, Rick DeNatale wrote:

For the newbies, note that %w{a b c} is an array literal equivalent to
[“a”, “b”, “c”] but more sparing on the fingers pushing the keys.

Something I’ve been wondering for a while, now . . .

Is there any particular reason that the traditional approach seems to be
to use %w{a b c} rather than %w[a b c] (the latter of which seems even
more sparing of the fingers)?

On 8/12/06, Robert K. [email protected] wrote:

``````     }
``````

This code looks completely useless to me. You end up with the same hash
whatever you do since there is no parametrization to your code. Where
is the point? Did I miss something?

I’m in agreement!

That complicated expression produces exactly the same results as

hash = { “foo” => {“x” => %w{a b c}, “y” => %w{x y z}}}

For the newbies, note that %w{a b c} is an array literal equivalent to
[“a”, “b”, “c”] but more sparing on the fingers pushing the keys.

And

x = []
%w{a b c}.each { | i | x << i }

is equivalent to
%w{a b c}.each { | i | i}

except for the side effect of appending all the elements to x, which
is then discarded anyway.

AND that last expression is equivalent to:
%w{a b c}

ALSO note that each actually returns the receiver of each, the block
might have side effects, but it normally doesn’t affect the result:

ar = %w{a b c}
ar.each{ | i | i + “X”} => [“a”, “b”, “c”]

There’s also a potential problem here because of object identity.

ar.equal?( ar.each{ | i | i}) => true

So the result of each is the same object, with the same object_id.
This probably isn’t a problem here because the literal in the original
expression isn’t referenced anywhere else. There cases in ruby where
unknowingly having two references to the same object can cause
suprising results when a change to the object made through one
reference can show up in other references:

a = b = %w{a b c}

a => [“a”, “b”, “c”]
b => [“a”, “b”, “c”]

a[1] = ‘q’
a => [“a”, “q”, “c”]
b => [“a”, “q”, “c”]

To have the result be based on the values of the block you need to use
another method from enumerable such as map

ar.map{ | i | i} => [“a”, “b”, “c”]

which produces a new array with a different identity.

ar.equal?( ar.map{ | i | i}) => false

But since we are using a block which is an identity transformation
here, a much clearer way to do this is to simply duplicate the array

ar.dup => [“a”, “b”, “c”]
ar.equal?(ar.dup) => false

Rick DeNatale

On Aug 12, 2006, at 2:01 PM, Chad P. wrote:

Is there any particular reason that the traditional approach seems
to be
to use %w{a b c} rather than %w[a b c] (the latter of which seems even
more sparing of the fingers)?

I’m a big fan of %w[ … ]. I love how it looks similar to [ … ],
only for words instead.

James Edward G. II

On 8/12/06, Chad P. [email protected] wrote:

Something I’ve been wondering for a while, now . . .

Is there any particular reason that the traditional approach seems to be
to use %w{a b c} rather than %w[a b c] (the latter of which seems even
more sparing of the fingers)?

Cause the bible done told us. Or at least because that’s what Dave,
or whoever wrote the examples in the Pickaxe used.

actually any of these should work:

%w(a b c)
%w
%w.a b c.
%wa b c

## The pickaxe says that %w1a b c1 should work but it doesn’t, it should say non-alphanumeric instead of nonalphabetic in the last pp on page 318.

Rick DeNatale

IPMS/USA Region 12 Coordinator
http://ipmsr12.denhaven2.com/

Visit the Project Mercury Wiki Site
http://www.mercuryspacecraft.com/

I like %w|foo bar| but also use %w{}. Maybe I should use %w[], though,
as it is an array, after all, huh?

Thanks for making me think different!

M.T.

Oh, and as a way to keep typing simple, any of these work… trippy!

%w’foor bar baz’
%w/foo bar baz/
%w.foo bar baz.
%w\foo bar baz
%w`foo bar baz`

This almost makes me feel bad. I think I’ll just stick to %w[] from now
on.

M.T.

more sparing of the fingers)?
I assume it’s individual preference. I use (square) brackets myself
for that reason.

I think some non-US keyboards allow braces without shifting.

Hal

## Is something like this possible? asdf = {“letters” => |||||some code that returns something|||||}

I’m fully aware of %w. Is most code w/ foo, abc & xyz useful to you?

x1:

Yes, you can do that.

asdf = { :letters => ( 1 == 1 ? ‘yes’ : ‘no’ ) }

That will give asdf the hash:

{ :letters => ‘yes’ }

I probably wouldn’t use a Proc for something like this… just doesn’t
seem to be appropriate. You could always define a quick little method
for evaluating a block inline if you wanted a particularly semantic
option:

def render
yield
end

used like:

a = { :letters => render { 1 == 1 ? ‘yes’ : ‘no’ } }

or, maybe you want something a little different…?

def pick a, b
((yield) ? a : b)
end

used like:

a = { :letters => pick(‘yes’,‘no’) { 1 == 1 } }

But that’s completely up to you.

M.T.

Matt T. wrote:

Oh, and as a way to keep typing simple, any of these work… trippy!

%w’foor bar baz’
%w/foo bar baz/
%w.foo bar baz.
%w\foo bar baz
%w`foo bar baz`

Don’t forget the pointy poke-your-eye-out brackets:

%w

Hal

As much as I like working in HTML…

%w

perhaps? Nightmares, I say. But, yes, it really is another good
alternative.

(But, a funny thought… what if we pulled out HTML and basically
eval’d a string like this with %w in front… instant array! Boom.)

Maybe I’m getting ahead of myself.

M.T.