简体   繁体   English

Ruby - 如何基于另一个布尔变量分配多个变量?

[英]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. 即对于这两个变量,我想要调用open_id_tag(2 params)否则open_tag(3 params)具体取决于true / false rec_id_line值。

You just have to put a space between rec_id_line and ? 你只需要在rec_id_line?之间放一个空格? :

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: 此外, line[2]=='@'可能会返回一个布尔值,因此可以简化第二行:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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