how to override [] brackets in ruby?

后端 未结 3 1849
灰色年华
灰色年华 2020-12-17 20:19

I am writing an Ajax request form with Ruby on Rails using a collection_select tag that looks like this:

<%= collection_select(\"Jobs\", \"cl         


        
3条回答
  •  臣服心动
    2020-12-17 20:59

    @German, tried to get answer for your question.

    2.1.3 :025 > class AntiArray < Array
    2.1.3 :026?>     def [](ind)
    2.1.3 :027?>         self.fetch(-ind) + 1
    2.1.3 :028?>     end
    2.1.3 :029?>   end
     => :[] 
    2.1.3 :030 > y = AntiArray.new([1,2,3,4])
     => [1, 2, 3, 4] 
    2.1.3 :031 > y[1]
     => 5 
    2.1.3 :032 > 
    

    Hope this is what you are asking for. Given a try for you

提交回复
热议问题