Magic First and Last Indicator in a Loop in Ruby/Rails

Magic First and Last Indicator in a Loop in Ruby/Rails?

You could grab the first and last elements and process them differently, if you like.

first = array.shift
last = array.pop
process_first_one
array.each { |x| process_middle_bits }
process_last_one

Discriminate first and last element in each?

One of the nicer approaches is:

@example.tap do |head, *body, tail|
head.do_head_specific_task!
tail.do_tail_specific_task!
body.each { |segment| segment.do_body_segment_specific_task! }
end

How to act differently on the first iteration in a Ruby loop?

You can do this:

my_array.each_with_index do |item, index|
if index == 0
# do something with the first item
end
# common stuff
end

Try it on ideone.

Tell the end of a .each loop in ruby

users.each_with_index do |u, index|
# some code
if index == users.size - 1
# code for the last user
end
end

Rails lists have .first and .second – is there a .hundredth or .sixty_nineth ?

There was a time when Rails added these, but there was a lot of controversy so most were removed. The only remnant of this experiment is Array#forty_two:

(1..100).to_a.forty_two
# => 42

array.select not iterating through every element

You need to remove the return statement from your method, Ruby uses implicit return (see https://jtrudell.github.io/blog/ruby_return_values/), so the result of a block is the last line that is evaluated in that block, the return statement in your code is treated as a return from the values method. Your method needs to look something like:

def values
@metric = metrics.select do |metric|
metric['href'].split('/').last == params[:id]
end
end

Getting the last x numbers from an infinite sequence in Ruby

Getting the last N numbers from a range -infinity..4 is the same of selecting a range of 4..(4-N).

4.downto(4-5).to_a
# => [4, 3, 2, 1, 0, -1]

You can package it as a custom method

def lastn(from, n)
from.downto(from-n).to_a
end

ruby: continue in same loop iteration after an exception

You're defining a begin-end block with a rescue clause. If any exception is raised by the block and there's a matching rescue clause, the block will stop executing and the rescue clause will be executed. If there's no matching rescue block, the error will bubble up (and hopefully be handled by another block, otherwise it'll be unhandled and your script will stop!) If there's an ensure clause, that'll get run too even if there's an exception.

So where does that leave us? If you want to defend against failure of individual steps and continue regardless, each step needs its own block:

3.times do |i|
begin
first_thing
rescue => e
puts "The first thing blew up! #{e.inspect}"
puts "I'll carry on anyway ¯\\_(ツ)_/¯"
end

begin
second_thing
third_thing
rescue => e
puts "Either the second or third thing blew up... #{e.inspect}"
puts "Straight on to the fourth thing!"
end

begin
fourth_thing
rescue => e
puts "Fourth thing blew up! #{e.inspect}"
end
end

It's a bit unusual to have a block like this which should carry on executing if something goes wrong - that's usually a good way to make one of the next steps go wrong too! You might need to make sure that you still have data integrity at each point, and that the later steps don't depend on something which might have failed to happen in the earlier steps.

Get string except first sentence

- i = article.content.index('.')

= article.content[0..i-1] # title
= article.content[i+1..-1] # content


Related Topics



Leave a reply



Submit