Say I have every loop in Ruby.
@list.each { |i| puts i if i > 10 break end }
Where I want to iterate over the list until the condition is met.
This stung me as "un Ruby'ish", and since I'm new to Ruby, is there any way Ruby can do this?
You can use Enumerable#detect or Enumerable#take_while , depending on the desired result.
Enumerable#detect
Enumerable#take_while
@list.detect { |i| puts i i > 10 } # Returns the first element greater than 10, or nil.
As others noted, the best style would be to first make your sub-choice and then act on it, for example:
@list.take_while{ |i| i <= 10 }.each{|i| puts i}
You can use take_while:
@list.take_while { |i| i <= 11 }.each { |i| puts i }
Source: https://habr.com/ru/post/1342530/More articles:Rails 3 the best way to realize the idea of ββkarma? - ruby-on-railsCreating Python GUI in eclipse? - pythonT-SQL: returning new INSERT identifier to C # - c #Facebook App Iframe OAuth Login / Permissions Error - Can I Avoid It? - facebookReplace elements in html template with values ββof C # object properties - htmlNeed help with binding Install with Spring MVC form - javaScala modified map add - scalaOne tuple element associated with a key type in Python - pythonStringFormat in wp7? - windows-phone-7What is a good technology stack for a Java based web application with REST support? - javaAll Articles