English 中文(简体)
我怎么能写一下对开关报表的评价?
原标题:How can I write lazy evaluating switch statements?
  • 时间:2009-11-18 17:06:32
  •  标签:
  • ruby

长期以来,我一直努力争取这项工作,我ve熟、我ve,我问一些当地鲁比主义者(尽管我确实应该这样做),但我还是问了。

我们正在努力做这样的工作:

case user.roles.included? (... magic ...) 
when [ admin ,  editor ] 
  then ...
when [ anonymous ] 
  then ...
end

因此,你可以看看它如何感觉到像<条码>><>>,但因为它没有使用<条码>=。 他指出,它没有工作。 我知道我可以使用<条码>,但认为应当使用<条码>。

最佳回答

。 反对时提出的论点。 在与<条码>不同行文中出现的“条码”时,也不必填写<条码>。 你们重新努力的正确准则是:

case x
when 16
  puts  hi 
when Object
  puts  obj 
end

关于新增加的问题:

case user.roles.included? (... magic ...) 
when [ admin ,  editor ] 
  ... 
when [ anonymous ] 
  ... 
end

这是因为Array s =。 页: 1 我不敢肯定Array s =的操作者来自哪里,甚至来自哪里,但你可以自行提供你所需要的功能。

根据上述代码判断,如果用户之一的作用与阵列相匹配,则请上<条码><>>>。 页: 1 Array#==仅作以下工作:

class Array
  def === other_array
    ! (other_array & self).empty?
  end
end

case user.roles
when [ admin ,  editor ] 
  ... 
when [ anonymous ] 
  ... 
end

<><>Caveat: 取决于您在何地凌驾? Array#=可能产生意外后果,因为它将改变这一范围的所有阵列。 鉴于=正在从中继承。 如果是<代码>=的别名,那么我不会期望它成为一个大问题。

=与旧=不同之处:

  • New === will return true if either array is a subset or reordering of the other.
  • Old === will only return true if the two arrays are identical (order and contents).

如我所知,case / >, =的唯一时间。 可以默示地要求一个阵列。

问题回答
case 
  when x == 16
    puts  hi 
  when x.is_a?(Object)
    puts  obj 
end

如果我真正理解这个问题,你就只需删除<条码>x<>/条码>在<条码>之后的变量。

伊斯兰法则不奏效,因为伊斯兰法只是回去了这一论点。

names_l.call(names)
# => {:name =>  Terry }

它喜欢实施以下法典:

case {:name =>  Terry }
  when  Terry 
    puts "Success!"
end

<代码>{:姓名 => Terry }不等于





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签