Help

J

Jagadeesh

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
 
J

Jeff Peng

14:25 +0900,Jagadeesh:
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


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"}]}
 
J

Jagadeesh

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


14:25 +0900,Jagadeesh:
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

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

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

Jeff Peng

在 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.
 
J

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

Jeff Peng

在 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.:)

Jeff.
 
J

Jagadeesh

在 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.:)

Jeff.

;)

Yes. I will
 
J

Jagadeesh

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

That answered me. Thanks for your time

Thanks
 
M

Marc Heiler

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

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

Marnen Laibow-Koser

Marc said:
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.
:) Use do/end for multiline blocks.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 

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


Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,426
Latest member
MrMet

Latest Threads

Top