If x.condition, set x to y

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!

wutang paul писал 04.07.2012 03:43:

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!

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.

Peter, that’s exactly what I was looking for. Much appreciated.

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”
end

Is 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 07/04/2012 11:59 AM, Ryan D. wrote:

No. It’s great.

You might want to look into using symbols for hash keys at some point… but
it’s not crucial.

Or you could do;

actor_data[‘biography’].empty?&& actor_data[‘biography’] = ‘Biography
not available’

Sam

Hi,

You should know though, that “||=” doesn’t really work with booleans. A
“false” will also be overwritten, because the operator doesn’t check
if the variable isn’t set but if it falsy (either nil or false). The
idea behind “a ||= b” is roughly “a = a || b” (like the other shortform
operators).

Robert K. wrote in post #1067316:

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

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!

On Wed, Jul 4, 2012 at 2:18 AM, Sam D. [email protected] wrote:

Or you could do;

actor_data[‘biography’].empty?&& actor_data[‘biography’] = ‘Biography not
available’

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

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