Simple question regarding hashes

X

x1

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
 
R

Robert Klemme

x1 said:
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

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
 
P

Peña, Botp

fr x1:
# can you define the value of a hash based on an iteration, or an if
# statement etc..

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

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

is that what you want?
kind regards -botp
 
X

x1

Yep.. Thanks alot. This some pretty neat stuff. :)

hash =3D {
"foo" =3D> Proc.new {
x =3D []
y =3D []
hash =3D {
"x" =3D> ["a", "b", "c"].each {|i| x << i},
"y" =3D> ["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

fr x1:
# can you define the value of a hash based on an iteration, or an if
# statement etc..

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

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

is that what you want?
kind regards -botp
 
R

Robert Klemme

x1 said:
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

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
 
R

Rick DeNatale

x1 said:
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

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
 
C

Chad Perrin

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)?
 
J

James Edward Gray II

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 Gray II
 
R

Rick DeNatale

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<a b c>
%w.a b c.
%w*a 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/
 
H

Hal Fulton

Chad said:
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)?

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
 
M

Matt Todd

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.
 
M

Matt Todd

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`

:D

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

M.T.
 
X

x1

As I said in my first example:
-----------------------------
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 said:
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

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
 
M

Matt Todd

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.
 
H

Hal Fulton

Matt said:
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`

:D

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

%w<foo bar baz>


Hal
 
M

Matt Todd

As much as I like working in HTML....

%w<xml-situps quantity="infinity" performance="poor">

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.
 

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

Similar Threads

Question Regarding Boolean Please 1
Regex replace problem 2
Tic Tac Toe Game 2
Help with pointers 1
Hashes 4
First time question 1
Hashes in 1.9 index 4
Array of Hashes in an array of hashes - Complicated! 16

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,687
Latest member
IngridXxj

Latest Threads

Top