bourg-ismael
bourg-ismael

Reputation: 93

Factorize condition with ruby

Is it possible to factorize this line with a ruby operator like ||= or something else ? :

if sheet.rows[start[:y]] then row = sheet.rows[start[:y]] else row = sheet.add_row [] end

Upvotes: 0

Views: 69

Answers (1)

shivam
shivam

Reputation: 16506

You can use ternary operator:

row = sheet.rows[start[:y]] ? sheet.rows[start[:y]] : sheet.add_row([])

Alternatively:

row = sheet.rows[start[:y]]
row = sheet.add_row [] if !row

Or (thanks to @tight):

row = sheet.rows[start[:y]] || sheet.add_row([])

Upvotes: 2

Related Questions