簡體   English   中英

Ruby - 如何基於另一個布爾變量分配多個變量?

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

我想做:

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)

這兩個方法都返回兩個值(順便說一句,我在這里重構)

即對於這兩個變量,我想要調用open_id_tag(2 params)否則open_tag(3 params)具體取決於true / false rec_id_line值。

你只需要在rec_id_line?之間放一個空格?

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

此外, line[2]=='@'可能會返回一個布爾值,因此可以簡化第二行:

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

或者結合兩條線:

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