0

I have this code:

all_backups = dir.entries[2..-1].sort.reverse
max_backups = 20
unwanted_backups = all_backups[max_backups..-1] || []

I think it gets all entries in a defined folder. What is [2..-1]? What is all_backups[max_backups..-1] || []?

2
  • Read about ranges: ruby-doc.org/core-2.2.0/Range.html Commented Jun 4, 2015 at 7:24
  • 2
    @Felix I'm sorry, but can you please clarify what you mean with your comment on @R_O_R's answer? By last 20, do you mean, the last 20 entries of all_backups or of unwanted_backups? And may I know what do you mean by oldest? Can you please tell where the oldest entries be located? (by way of array index) Commented Jun 4, 2015 at 7:45

1 Answer 1

1
dir.entries[2..-1]

get elements from index 2(means 3rd element of your array) to last index(last element of your array). In Ruby -1 means last element of the Array instance.

all_backups[max_backups..-1] || []

all_backups[max_backups..-1] gives nil, then assign with an empty array [] to the variables other wise returned array from all_backups[max_backups..-1].

Sign up to request clarification or add additional context in comments.

1 Comment

thanks. What I want to do: I make mysql dumps from a database and I want to save the last 20 backups. But with the code above 20 backups were saved, when the 21 is going to save all 20 backups will be deleted .. The oldest one should be deleted ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.