Rails这行代码做了什么

时间:2017-08-23 21:02:14

标签: ruby-on-rails

任何人都可以向我解释这行代码

<% child_nodes = node.is_a?(Department) ? node.offices.where(parent_office_id: nil) : node.branch_offices %>

我感到困惑? :
这叫什么? :

3 个答案:

答案 0 :(得分:1)

所以这个语句包含一个三元运算符:child_nodes = node.is_a?(Department) ?

如果评估为真,则执行第一个条件,即:node.offices.where(parent_office_id: nil)

否则运行此代码:node.branch_offices

希望这有帮助!

答案 1 :(得分:1)

它被称为三元运算符,其格式为if

您可以使用else<% if node.is_a?(Department) child_nodes = node.offices.where(parent_office_id: nil) else child_nodes = node.branch_offices end %> 来撰写。

//Render tiles
            for (int i = 0; i < blockX.size(); i++) {
                    block.setPosition(float(blockX[i]), float(blockY[i]));
                    if (blockType[i] == "metal") {
                            //if block is metal tile
                            block.setTexture(metalBlockSingle);
                            if (doesBlockExist(blockX[i] + 20, blockY[i])) {
                                    //if block is to right
                                    if (doesBlockExist(blockX[i], blockY[i] + 20)) {
                                            //if block is below
                                            if (doesBlockExist(blockX[i] - 20, blockY[i])) {
                                                    //if block is to left
                                                    if (doesBlockExist(blockX[i], blockY[i] - 20)) {
                                                            //if block is above
                                                            block.setTexture(metalBlockCenter);
                                                    }
                                                    else {
                                                            block.setTexture(metalBlockBottomRightLeft);
                                                    }
                                            }
                                            else if (doesBlockExist(blockX[i], blockY[i] - 20)) {
                                                    //if block is above
                                                    block.setTexture(metalBlockTopBottomRight);
                                            }
                                            else {
                                                    //if block is neither above nor to left
                                                    block.setTexture(metalBlockBottomRight);
                                            }
                                    }
                                    else if (doesBlockExist(blockX[i] - 20, blockY[i])) {
                                            //if block is to left
                                            if (doesBlockExist(blockX[i], blockY[i] - 20)) {
                                                    //if block is above
                                                    block.setTexture(metalBlockTopRightLeft);
                                            }
                                            else {
                                                    block.setTexture(metalBlockRightLeft);
                                            }
                                    }
                                    else if (doesBlockExist(blockX[i], blockY[i] - 20)) {
                                            //if block is above
                                            block.setTexture(metalBlockTopRight);
                                    }
                                    else {
                                            //if block is only right
                                            block.setTexture(metalBlockRight);
                                    }
                            }
                            else if (doesBlockExist(blockX[i], blockY[i] + 20)) {
                                    //if block is below
                                    if (doesBlockExist(blockX[i] - 20, blockY[i])) {
                                            //if block is to left
                                            if (doesBlockExist(blockX[i], blockY[i] - 20)) {
                                                    //if block is above
                                                    block.setTexture(metalBlockTopBottomLeft);
                                            }
                                            else {
                                                    //if block is not above
                                                    block.setTexture(metalBlockBottomLeft);
                                            }
                                    }
                                    else {
                                            //if block is not to left
                                            if (doesBlockExist(blockX[i], blockY[i] - 20)) {
                                                    //if block is above
                                                    block.setTexture(metalBlockTopBottom);
                                            }
                                            else {
                                                    //if metal block is not above
                                                    block.setTexture(metalBlockBottom);
                                            }
                                    }
                            }
                            else if (doesBlockExist(blockX[i] - 20, blockY[i])) {
                                    //if block is to left
                                    if (doesBlockExist(blockX[i], blockY[i] - 20)) {
                                            //if block is above
                                            block.setTexture(metalBlockTopLeft);
                                    }
                                    else {
                                            block.setTexture(metalBlockLeft);
                                    }
                            }
                            else if (doesBlockExist(blockX[i], blockY[i] - 20)) {
                                    //if block is above
                                    block.setTexture(metalBlockTop);
                            }
                    }
                    window.draw(block);
            }

答案 2 :(得分:0)

它是一家三元运营商。写这个的另一种方法是:

<%
  if node.is_a?(Department)
    child_nodes = node.offices.where(parent_office_id: nil)
  else
    child_nodes = node.branch_offices
  end
%>