Help

Hi,

DATA = {}
prs = [
457678,457678-3,open
457678,457678-5,closed
457678,457678-6,open
]

prs.each do |line|
(pr, pr_scope, state) = line.split(/,/)
DATA[pr] = Hash[state, pr_scope]
end

But when I pring DATA, its having 457678,457678-6,open line. I am
overwriting earlier values with last values. What am I doing wrong?

Thanks

14:25 +0900,Jagadeesh:

end
What you wanted is maybe:

~/tmp$ cat hash.rb
prs = [
“457678,457678-3,open”,
“457678,457678-5,closed”,
“457678,457678-6,open”,
]

data = Hash.new do |h,k| h[k] = [] end

prs.each do |line|
(pr, pr_scope, state) = line.split(/,/)
data[pr] << Hash[state, pr_scope]
end

p data

~/tmp$ ruby hash.rb
{“457678”=>[{“open”=>“457678-3”}, {“closed”=>“457678-5”},
{“open”=>“457678-6”}]}

Thanks Jeff. yes you are correct. But have a question

On Jan 11, 11:04Â am, Jeff P. [email protected] wrote:

  (pr, pr_scope, state) = line.split(/,/)
]

data = Hash.new do |h,k| h[k] = [] end

I added something like
y = lambda { |h,k| h[k] = Hash.new(&y) }

$DATA = Hash.new(&y)

But I do not understood why to add
y = lambda { |h,k| h[k] = Hash.new(&y) }

Please explain. I am newbie to Ruby and coming from Perl.

在 2010-01-11一的 15:30 +0900,Jagadeesh写道:

Thanks Jeff. yes you are correct. But have a question

Because ruby is a strongly typed language while perl isn’t.

In perl everything doesn’t need to be pre-defined with specified type.
for example, a hash:

my %hash;
push @{$hash{key1}},“abc”;

As you see above, though we didn’t pre-define $hash{key1} as an
anonymous array, but perl does that for us silently.

But in ruby, you can’t say:

hash = Hash.new
hash[‘key1’] << “abc”

This will get an error.
Instead we would say:

hash=Hash.new do |h,k| h[‘k’] = [] end
hash[‘key1’] << “abc”

This will work because we have passed a block to Hash.new method, this
block will set hash’s every value to a seperate empty array []. After
that the array filling ("<<" in ruby) can run.

HTH.

Jeff.

Hi Jeff,

data = Hash.new do |h,k| h[k] = [] end

Another interesting question is, what is standard way of writing one
liner code and multiliner

I read somewhere like oneliner should be written using {}
and do … end for multiliner

For example:

one liner:

data = Hash.new { |h,k| h[k] = [] }

and multiliner:

prs.each do |line|
(pr, pr_scope, state) = line.split(/,/)
data[pr] << Hash[state, pr_scope]
end

在 2010-01-11一的 16:00 +0900,Jagadeesh写道:

Hi Jeff,

data = Hash.new do |h,k| h[k] = [] end

Another interesting question is, what is standard way of writing one
liner code and multiliner

I read somewhere like oneliner should be written using {}
and do … end for multiliner

For oneliner I just think both them are the same.
Just take the one you like to write.:slight_smile:

Jeff.

El Lunes, 11 de Enero de 2010, Jagadeesh
escribió:> Hi,

Please, use a descriptive subject for your mail rather than just “Help”.
Thanks.

On Jan 11, 12:05Â pm, Jeff P. [email protected] wrote:

and do … end for multiliner

For oneliner I just think both them are the same.
Just take the one you like to write.:slight_smile:

Jeff.

:wink:

Yes. I will

On Jan 11, 1:48 pm, Iñaki Baz C. [email protected] wrote:

El Lunes, 11 de Enero de 2010, Jagadeesh escribió:

Hi,

Please, use a descriptive subject for your mail rather than just “Help”.
Thanks.


Iñaki Baz C. [email protected]

Okay

On Jan 11, 11:54Â am, Jeff P. [email protected] wrote:

push @{$hash{key1}},“abc”;
Instead we would say:
Jeff.
That answered me. Thanks for your time

Thanks

For oneliner I just think both them are the same.
Just take the one you like to write.:slight_smile:

I can agree with the statement, at least partially, but I must say that
in oneliners I really prefer the {} notation compared to do/end on one
line.

Also, I found myself doing this sometimes:

def some_method
array.map! {|i|
call_method_one
call_method_one
another_method_here
i.strip
}
end

And so forth. I am aware that smaller methods are usually better than
long methods, but in the case where I really have long methods, I came
to appreciate using {} instead of do/end, because I like to see the
closing } as finishing that statement.

Compare this to this code:

def some_method
array.map! do |i|
call_method_one
call_method_one
another_method_here
i.strip
end
end

I don’t really like the two ends. The } makes my eyes happy, and my poor
brain happy as well, and if Ruby doesn’t mind either way then I try to
stick to the first method.

Marc H. wrote:

For oneliner I just think both them are the same.
Just take the one you like to write.:slight_smile:

I can agree with the statement, at least partially, but I must say that
in oneliners I really prefer the {} notation compared to do/end on one
line.

Yes, that’s standard style.

Also, I found myself doing this sometimes:

def some_method
array.map! {|i|
call_method_one
call_method_one
another_method_here
i.strip
}
end

And so forth. I am aware that smaller methods are usually better than
long methods, but in the case where I really have long methods, I came
to appreciate using {} instead of do/end, because I like to see the
closing } as finishing that statement.

That is nonstandard style. Ruby isn’t supposed to look like C or Java.
:slight_smile: Use do/end for multiline blocks.

Best,
–Â
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Compare this to this code:

def some_method
array.map! do |i|
call_method_one
call_method_one
another_method_here
i.strip
end
end

I don’t really like the two ends. The } makes my eyes happy, and my poor
brain happy as well, and if Ruby doesn’t mind either way then I try to
stick to the first method.