比较运算符'=='如何与以下代码的'if'语句一起使用?

时间:2019-04-10 18:44:46

标签: ruby

如以下方法所示,我不了解“比较运算符”的工作方式。如果定义的“堆栈数组”最初为空,那么如何将散列“ OPPOSITE”的元素与空数组的最后一个元素进行比较?比较是否发生在操作数“ OPPOSITE [dir]”上?即使这样,仍然不清楚。

谢谢

代码:

 OPPOSITE = {
   "NORTH" => "SOUTH",
   "SOUTH" => "NORTH",
   "EAST"  => "WEST",
   "WEST"  => "EAST"
 }

def dirReduc(arr)
  stack = []

  arr.each do |dir|
    OPPOSITE[dir] == stack.last ? stack.pop : stack.push(dir)
  end

  stack
end 


 arr = ["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]

 print dirReduc(arr)

1 个答案:

答案 0 :(得分:2)

<Style x:Key="Collapse" TargetType="StackPanel"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsEnabled}" Value="true"> <DataTrigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="Width" Duration="0:0:1" From="640" To="320"/> </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> </DataTrigger> </Style.Triggers> </Style> <Style x:Key="Expand" TargetType="StackPanel"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=IsVisible}" Value="true"> <DataTrigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="Width" Duration="0:0:1" From="320" To="640"> </DoubleAnimation> </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> </DataTrigger> </Style.Triggers> </Style> 为空时,stack将返回stack.last

nil将返回OPPOSITE['NORHT']'SOUTH',如果'SOUTH' == nil,则将评估false分支(else)。