简体   繁体   中英

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)

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM