[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Simple question regarding hashes

x1

8/11/2006 3:01:00 AM

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

15 Answers

Robert Klemme

8/11/2006 6:05:00 AM

0

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

Peña, Botp

8/11/2006 6:47:00 AM

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 = {"foo" =>Proc.new {1+1 == 3 ? "yes" : "no"}}
=> {"foo"=>#<Proc:0x02b0f124@(irb):3>}
irb(main):005:0> hash["foo"]
=> #<Proc:0x02b0f124@(irb):3>
irb(main):006:0> hash["foo"].call
=> "no"

is that what you want?
kind regards -botp

x1

8/12/2006 4:14:00 AM

0

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 8/11/06, Peña, Botp <botp@delmonte-phil.com> wrote:
> 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 = {"foo" =>Proc.new {1+1 == 3 ? "yes" : "no"}}
> => {"foo"=>#<Proc:0x02b0f124@(irb):3>}
> irb(main):005:0> hash["foo"]
> => #<Proc:0x02b0f124@(irb):3>
> irb(main):006:0> hash["foo"].call
> => "no"
>
> is that what you want?
> kind regards -botp
>
>

Robert Klemme

8/12/2006 6:49:00 AM

0

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

Rick DeNatale

8/12/2006 6:55:00 PM

0

On 8/12/06, Robert Klemme <shortcutter@googlemail.com> wrote:
> x1 wrote:
> > 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

--
Rick DeNatale

http://talklikeaduck.denh...

Chad Perrin

8/12/2006 7:02:00 PM

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

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"There comes a time in the history of any project when it becomes necessary
to shoot the engineers and begin production." - MacUser, November 1990

James Gray

8/12/2006 7:09:00 PM

0

On Aug 12, 2006, at 2:01 PM, Chad Perrin 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 Gray II

Rick DeNatale

8/12/2006 10:50:00 PM

0

On 8/12/06, Chad Perrin <perrin@apotheon.com> 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<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.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Hal E. Fulton

8/12/2006 11:49:00 PM

0

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

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




Matt Todd

8/13/2006 2:17:00 AM

0

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.