if (@product_coverages != nil && @product_coverages.length > 0) @product_coverages = @product_coverages.each do
product_coverage|
product_coverage.external_description =
Coverage.find(product_coverage.coverage_id).external_description # test
if (product_coverage.external_description != nil)
product_coverage.external_description.capitalize!
end
end
end
Source B :
if (@product_coverages != nil && @product_coverages.length > 0) @product_coverages = @product_coverages.each do
product_coverage|
product_coverage.external_description =
Coverage.find(product_coverage.coverage_id).external_description # test
return "" if product_coverage.external_description.nil?
product_coverage.external_description.capitalize!
end
end
I wanted to use Source B, but it return "" everytime. But Source A
works. I expected both of them to work.
Put in a test print, to see what nil? is returning. It is possible for
a class to override nil?, though it's hard to think of why one would.
martin
···
On 12/6/06, Jason Vogel <jasonvogel@gmail.com> wrote:
Source A :
if (@product_coverages != nil && @product_coverages.length > 0) @product_coverages = @product_coverages.each do
>product_coverage|
product_coverage.external_description =
Coverage.find(product_coverage.coverage_id).external_description # test
if (product_coverage.external_description != nil)
product_coverage.external_description.capitalize!
end
end
end
Source B :
if (@product_coverages != nil && @product_coverages.length > 0) @product_coverages = @product_coverages.each do
>product_coverage|
product_coverage.external_description =
Coverage.find(product_coverage.coverage_id).external_description # test
return "" if product_coverage.external_description.nil?
product_coverage.external_description.capitalize!
end
end
I wanted to use Source B, but it return "" everytime. But Source A
works. I expected both of them to work.
if (@product_coverages != nil && @product_coverages.length > 0) @product_coverages = @product_coverages.each do
>product_coverage|
product_coverage.external_description =
Coverage.find(product_coverage.coverage_id).external_description # test
if (product_coverage.external_description != nil)
product_coverage.external_description.capitalize!
end
end
end
Source B :
if (@product_coverages != nil && @product_coverages.length > 0) @product_coverages = @product_coverages.each do
>product_coverage|
product_coverage.external_description =
Coverage.find(product_coverage.coverage_id).external_description # test
return "" if product_coverage.external_description.nil?
product_coverage.external_description.capitalize!
end
end
I wanted to use Source B, but it return "" everytime. But Source A
works. I expected both of them to work.
Jason
if @product_coverages != nil @product_coverages.each { |p_c|
p_c.external_description =
Coverage.find(p_c.coverage_id).external_description and
p_c.external_description.capitalize!
}
end
···
-------------
A simpler way. When a description that's nil is encountered,
replace it with "".
if @product_coverages != nil @product_coverages.each { |p_c|
p_c.external_description =
(Coverage.find(p_c.coverage_id).external_description or "").
capitalize
}
end
If you put those source in a loop, source B always return "" if
product_coverage.external_description == nil while source A does not.
Basically those sources do different job .
···
On 12/6/06, Martin DeMello <martindemello@gmail.com> wrote:
On 12/6/06, Jason Vogel <jasonvogel@gmail.com> wrote:
> Source A :
>
> if (@product_coverages != nil && @product_coverages.length > 0)
> @product_coverages = @product_coverages.each do
> >product_coverage|
> product_coverage.external_description =
> Coverage.find(product_coverage.coverage_id).external_description # test
> if (product_coverage.external_description != nil)
> product_coverage.external_description.capitalize!
> end
> end
> end
>
> Source B :
>
> if (@product_coverages != nil && @product_coverages.length > 0)
> @product_coverages = @product_coverages.each do
> >product_coverage|
> product_coverage.external_description =
> Coverage.find(product_coverage.coverage_id).external_description # test
> return "" if product_coverage.external_description.nil?
> product_coverage.external_description.capitalize!
> end
> end
>
> I wanted to use Source B, but it return "" everytime. But Source A
> works. I expected both of them to work.
Put in a test print, to see what nil? is returning. It is possible for
a class to override nil?, though it's hard to think of why one would.
martin
--
Chỉ có lý trí của bản thân mới soi sáng được sự thật."Cái đẹp đẽ nhất mà
chúng ta có thể trải nghiệm được là cái bí ẩn. Đó là cảm thức nền tảng trong
cái nôi của nghệ thuật và khoa học chân chính. Kẻ nào không biết đến nó,
không còn khả năng ngạc nhiên hay kinh ngạc, kẻ đó coi như đã chết, đã tắt
rụi lửa sống trong mắt mình""Das Schönste, was wir erleben können, ist das
Geheimnisvolle. Es ist das Grundgefühl, das an der Wiege von wahrer Kunst
und Wissenschaft steht. Wer es nicht kennt und sich nicht mehr wundern,
nicht mehr staunen kann, der ist sozusagen tot und sein Auge erloschen.""The
fairest thing we can experience is the mysterious. It is the fundamental
emotion which stands at the cradle of true art and true science. He who
knows it not and can no longer wonder, no longer feel amazement, is as good
as dead, his eyelight has been extinct."
I found 'return "" if product_coverage.external_description.nil?' as a
source sample somewhere, and I liked the readability factor. It seemed
very "Ruby-ish" to a PowerBuilder/Java/C# developer :-).
In the end, I'm trying to figure out my "best practice" for iterating
over an array of objects. I'll happily take any suggestions.
Thanks,
Jason
···
On Dec 6, 2:31 pm, "James Moore" <bans...@banshee.com> wrote:
Is there a particular reason you're comparing to nil? Normally I'd just
write something like
This is Perlish, and there's people (myself included) that don't like
the style - it hides the nil-check, which is useful to be more visible
if you're doing it as a special case filter, like snippet B in the
original post does. I usually prefer getting rid of special cases like
missing arguments etc. with a series of checks and returns / exceptions
in the worse cases at a the beginning of a method to keep precondition
verification in one place instead of scattered through the code with
failure consequences interspersed with regular processing.
I appreciate the advice. I've just being trying to be careful to not
be too C#/Java-ish. I've never done any Perl.
Thanks,
Jason
···
On Dec 7, 12:55 am, David Vallner <d...@vallner.net> wrote:
James Moore wrote:
> Is there a particular reason you're comparing to nil? Normally I'd just
> write something like
> (@product_coverages && @product_coverages.length > 0)This is Perlish, and there's people (myself included) that don't like
the style - it hides the nil-check, which is useful to be more visible
if you're doing it as a special case filter, like snippet B in the
original post does. I usually prefer getting rid of special cases like
missing arguments etc. with a series of checks and returns / exceptions
in the worse cases at a the beginning of a method to keep precondition
verification in one place instead of scattered through the code with
failure consequences interspersed with regular processing.