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.
On Jan 11, 11:54 am, Jeff Peng <jeffp...@netzero.net> wrote:
在 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.
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.
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.
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.
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.