Skip to Main Content
Ruby. Receptury
book

Ruby. Receptury

by Lucas Carlson, Leonard Richardson
November 2012
Intermediate to advanced content levelIntermediate to advanced
888 pages
54h 46m
Polish
Helion
Content preview from Ruby. Receptury

6.14. Tworzenie wersjonowanych kopii pliku

Problem

Przed nadpisaniem zawartości oryginalnego pliku chcemy utworzyć jego kopię; postępowanie to chcemy wielokrotnie powtarzać, za każdym razem nadając docelowemu plikowi inną nazwę, jednak bazującą na nazwie oryginalnej.

Rozwiązanie

Nazwę kolejnej kopii zapasowej możemy tworzyć przez dołączenie do nazwy oryginalnej przyrostka identyfikującego kolejną wersję. Począwszy od pewnego przyrostka „startowego”, na przykład '.0', za pomocą metody String#succ możemy generować kolejne nazwy aż do uzyskania nazwy pliku nieistniejącego.

class File def File.versioned_filename(base, first_suffix='.0') suffix = nil filename = base while File.exists?(filename) suffix = (suffix ? suffix.succ : first_suffix) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ReasonML Quick Start Guide

ReasonML Quick Start Guide

Raphael Rafatpanah, Bruno Joseph D'mello
Microsoft Excel: Formeln & Funktionen - Das Maxibuch, 3., aktualisierte und erweiterte Auflage

Microsoft Excel: Formeln & Funktionen - Das Maxibuch, 3., aktualisierte und erweiterte Auflage

Egbert Jeschke, Eckehard Pfeifer, Helmut Reinke, Sara Unverhau, Bodo Fienitz
Mastering Ruby Closures

Mastering Ruby Closures

Benjamin Tan Wei Hao

Publisher Resources

ISBN: 9781457177453Errata Page