[yast-devel] Re: [yast-commit] <web-client> master : use lambdas instead loops
* Josef Reidinger <jreidinger@suse.cz> [Aug 04. 2009 10:00]:
+ region = timezones.find { |reg| reg.name == params[:region] } + region = {} unless region
This can be further shortened by writing region = (timezones.find { |reg| reg.name == params[:region] }) || {} Where I prefer region = (timezones.find { |reg| reg.name == params[:region] }) || Hash.new to make it less cryptic ;-) Klaus --- SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
On 08/04/2009 10:07 AM, Klaus Kaempf wrote:
* Josef Reidinger <jreidinger@suse.cz> [Aug 04. 2009 10:00]:
+ region = timezones.find { |reg| reg.name == params[:region] } + region = {} unless region
This can be further shortened by writing
region = (timezones.find { |reg| reg.name == params[:region] }) || {}
Where I prefer
region = (timezones.find { |reg| reg.name == params[:region] }) || Hash.new
to make it less cryptic ;-)
Thanks, you are right...and exist some trick to get return value of find and if nil then call another function like a.find() ?? .id so result will be id or nil? Josef -- Josef Reidinger YaST team maintainer of perl-Bootloader, YaST2-Repair, webyast modules language and time -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
* Josef Reidinger <jreidinger@suse.cz> [Aug 04. 2009 10:15]:
On 08/04/2009 10:07 AM, Klaus Kaempf wrote:
Where I prefer
region = (timezones.find { |reg| reg.name == params[:region] }) || Hash.new
to make it less cryptic ;-)
Thanks, you are right...and exist some trick to get return value of find and if nil then call another function like a.find() ?? .id so result will be id or nil?
Look at Enumerable#detect (find is an alias to detect) (http://ruby-doc.org/core/classes/Enumerable.html#M003123). Klaus --- SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg) -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
On Tuesday 04 August 2009 10:07:41 Klaus Kaempf wrote:
* Josef Reidinger <jreidinger@suse.cz> [Aug 04. 2009 10:00]:
+ region = timezones.find { |reg| reg.name == params[:region] } + region = {} unless region
This can be further shortened by writing
region = (timezones.find { |reg| reg.name == params[:region] }) || {}
Where I prefer
region = (timezones.find { |reg| reg.name == params[:region] }) || Hash.new
to make it less cryptic ;-)
Heh, I think the || is pretty readable. You can even use the keyword 'or' and make it more english: region = (timezones.find { |reg| reg.name == params[:region] }) or {} I prefer [ ] and { } more than Array.new and Hash.new ;-) -- Duncan Mac-Vicar P. - Engineering Manager, YaST SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nuernberg) -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
On 08/04/2009 05:09 PM, Duncan Mac-Vicar Prett wrote:
On Tuesday 04 August 2009 10:07:41 Klaus Kaempf wrote:
* Josef Reidinger <jreidinger@suse.cz> [Aug 04. 2009 10:00]:
+ region = timezones.find { |reg| reg.name == params[:region] } + region = {} unless region
This can be further shortened by writing
region = (timezones.find { |reg| reg.name == params[:region] }) || {}
Where I prefer
region = (timezones.find { |reg| reg.name == params[:region] }) || Hash.new
to make it less cryptic ;-)
Heh, I think the || is pretty readable. You can even use the keyword 'or' and make it more english: region = (timezones.find { |reg| reg.name == params[:region] }) or {}
I prefer [ ] and { } more than Array.new and Hash.new ;-)
I think that it is not so important as consistency. For reader is not problem to read both type of syntax, but it is quite problematic if it is mixed. That confuses reader, so I have no problem with both syntax, but we should decide for one and use it unless we have good reason to break consistency (if exist good reason and reader see it that it is OK). -- Josef Reidinger YaST team maintainer of perl-Bootloader, YaST2-Repair, webyast modules language and time -- To unsubscribe, e-mail: yast-devel+unsubscribe@opensuse.org For additional commands, e-mail: yast-devel+help@opensuse.org
participants (3)
-
Duncan Mac-Vicar Prett
-
Josef Reidinger
-
Klaus Kaempf