Michael Durrant
Michael Durrant

Reputation: 96454

Ruby - how to assign multiple variables based on another boolean variable?

I am trying to do:

the_tag= line[2..5]
rec_id_line = (line[2]=='@')? true : false

new_contents,new_to_close= 
  rec_id_line? open_id_tag(indent,line) : open_tag(indent,the_tag,last_field)

Those two methods both return two values (btw I'm refactoring here)

i.e. for the two variables, I either want to call open_id_tag(2 params) otherwise open_tag(3 params) depending on the true/false rec_id_line value.

Upvotes: 0

Views: 258

Answers (1)

Stefan
Stefan

Reputation: 114138

You just have to put a space between rec_id_line and ?:

new_contents, new_to_close = rec_id_line ? open_id_tag(indent, line) : open_tag(indent, the_tag, last_field)

Furthermore line[2]=='@' probably returns a boolean value so can simplify your second line:

rec_id_line = (line[2] == '@')

Or combine both lines:

new_contents, new_to_close = (line[2] == '@') ? open_id_tag(indent, line) : open_tag(indent, the_tag, last_field)

Upvotes: 2

Related Questions