From: "Martin J. Dürst" Date: 2010-10-08T16:16:34+09:00 Subject: [ruby-core:32718] Re: [Ruby 1.9-Feature#3845][Open] "in" infix operator Hello Yusuke, On 2010/10/05 23:28, Yusuke ENDOH wrote: > Hi, > > 2010/9/24 "Martin J. D�rst": >> Just my two cents, but I don't see why this case is important enough to >> warrant deviating from the usual, object-oriented syntax (which may not >> always look optimal, but is easy and straightforward). > > > Thank you for your comment. > > Indeed, an idiom [a, b, c].include?(x) can be used as a substitute. > However, it has three problems: > > 1) the word order is weird; x should appear first (at least, many > people feel so) I would understand that if it were [a, b, c].included? x But it's include?, so the order seems just fine. Easy to read as "does [a, b, c] include x?". Any other order would feel strange, wouldn't it? Also, an 'in?' method on Object has been proposed, so that you can write x.in? [a, b, c] That's very short, and fully object oriented pure Ruby, no syntactic sugar necessary. > 2) the idiom is too long (even though it is often used) I don't think Ruby method names are optimized according to usage frequency. And where is "too long"? 'include?' is 8 characters. [If it is really too long, what about maybe 'incl?'? (I don't like that personally, but just in case.)] > 3) it is inefficient; new array object is created every times That's a problem for a good compiler/interpreter. There are many cases in Ruby where similar stuff happen, and nevertheless, many people are using Ruby. If it really needs to be fast, why not use C or so? > Also, x == a || x == b || x == c can be used. It has another problem: > it becomes very verbose when "x" is long. > > http_request.http_method == :get || > http_request.http_method == :post || > http_request.http_method == :put || > http_request.http_method == :delete > > vs. > > http_request.http_method in :get, :post, :put, :delete Well, [:get, :post, :put, :delete].include?(http_request.http_method) is still available. Just use that. As for the 'new array object created every time' problem, the best thing here would be to define something like: HTTP::REST_METHODS = [:get, :post, :put, :delete] and then later just do: HTTP::REST_METHODS.include?(http_request.http_method) or so. Another way to do it would be: case http_request.http_method when :get, :post, :put, :delete ... end > What is worse is that we often want to write this kind of code. > If we rarely wrote this kind of code, I would think that new syntax > was not needed. There are many other methods in Ruby that are used very often. Do we want to create special syntax for all of them? Regards, Martin. -- #-# Martin J. D�rst, Professor, Aoyama Gakuin University #-# http://www.sw.it.aoyama.ac.jp mailto:duerst@it.aoyama.ac.jp