Hi, big time newbie here.
if actor_data['biography'].empty?
actor_data['biography'] = "Biography not available"
end
Is there a shorter/better way to do this?
Thanks for your help!
···
--
Posted via http://www.ruby-forum.com/.
Hi, big time newbie here.
if actor_data['biography'].empty?
actor_data['biography'] = "Biography not available"
end
Is there a shorter/better way to do this?
Thanks for your help!
--
Posted via http://www.ruby-forum.com/.
wutang paul писал 04.07.2012 03:43:
Hi, big time newbie here.
if actor_data['biography'].empty?
actor_data['biography'] = "Biography not available"
endIs there a shorter/better way to do this?
Thanks for your help!
if actor_data['biography'].nil?
actor_data['biography'] = "Biography not available"
end
is the same as
actor_data['biography'] ||= "Biography not available"
There is no shortcut for #empty? that I know, and probably no at all.
--
WBR, Peter Zotov.
Peter, that's exactly what I was looking for. Much appreciated.
--
Posted via http://www.ruby-forum.com/.
Hi, big time newbie here.
Welcome
if actor_data['biography'].empty?
actor_data['biography'] = "Biography not available"
endIs there a shorter/better way to do this?
No. It's great.
You might want to look into using symbols for hash keys at some point... but it's not crucial.
On Jul 3, 2012, at 16:43 , wutang paul wrote:
Hi,
You should know though, that "||=" doesn't really work with booleans. A
"false" will also be overwritten, because the expression doesn't check
if the variable isn't set but if it falsy (either nil or false).
--
Posted via http://www.ruby-forum.com/.
Or you could do;
actor_data['biography'].empty?&& actor_data['biography'] = 'Biography not available'
Sam
On 07/04/2012 11:59 AM, Ryan Davis wrote:
On Jul 3, 2012, at 16:43 , wutang paul wrote:
Hi, big time newbie here.
Welcome
if actor_data['biography'].empty?
actor_data['biography'] = "Biography not available"
endIs there a shorter/better way to do this?
No. It's great.
You might want to look into using symbols for hash keys at some point... but it's not crucial.
Only that this won't work if actor_data is a Hash without default
value because NilClass#empty? does not exist:
irb(main):002:0> {}['biography'].empty?
NoMethodError: undefined method `empty?' for nil:NilClass
from (irb):2
from /opt/bin/irb19:12:in `<main>'
Methinks this is actually the best and most idiomatic way to do it:
actor_data['biography'] ||= 'Biography not available'
Better learn common idioms right from the start.
I do have another issue with this though: if that string is placed in
the Hash the information is lost that this value wasn't initially
available. IMHO it's much better to do the replacement when printing,
e.g.
%w{name biography country}.each do |field|
printf "%-20s: %s\n", field, actor_data[field] || "#{field} not available"
end
or just
%w{name biography country}.each do |field|
printf "%-20s: %s\n", field, actor_data[field] || 'not available'
end
Btw wutang, IRB is a great too to try these things out.
Kind regards
robert
On Wed, Jul 4, 2012 at 2:18 AM, Sam Duncan <sduncan@wetafx.co.nz> wrote:
Or you could do;
actor_data['biography'].empty?&& actor_data['biography'] = 'Biography not
available'
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Robert Klemme wrote in post #1067316:
%w{name biography country}.each do |field|
printf "%-20s: %s\n", field, actor_data[field] || "#{field} not
available"
endor just
%w{name biography country}.each do |field|
printf "%-20s: %s\n", field, actor_data[field] || 'not available'
endBtw wutang, IRB is a great too to try these things out.
I like your approach Robert, it would work particularly well for my use
case. Thanks for the reminder about IRB too, I was wasting time editing,
running and outputting to console. I'll get the hang of this wonderful
language eventually!
--
Posted via http://www.ruby-forum.com/.