This shows you the differences between two versions of the page.
| Previous revision | |||
| — | fuss:ruby [2025/10/21 23:26] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Reverse List ====== | ||
| + | <code ruby> | ||
| + | ########################################################################### | ||
| + | ## Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 ## | ||
| + | ########################################################################### | ||
| + | def wasListReverse(list = []) | ||
| + | if list.length <= 1; return list; end | ||
| + | return wasListReverse(list[1..-1]) + [ list[0] ] | ||
| + | end | ||
| + | |||
| + | </ | ||
| + | |||
| + | ====== Map Preserving Sort using Quicksort for Integers ====== | ||
| + | |||
| + | <code ruby> | ||
| + | ########################################################################### | ||
| + | ## Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 ## | ||
| + | ########################################################################### | ||
| + | def wasDualQuicksort(a = [], b = []) | ||
| + | raise(" | ||
| + | if a.length <= 1 | ||
| + | return a + b | ||
| + | end | ||
| + | | ||
| + | pivot_a = a[0] | ||
| + | a.shift | ||
| + | pivot_b = b[0] | ||
| + | b.shift | ||
| + | | ||
| + | less = [] | ||
| + | less_b = [] | ||
| + | more = [] | ||
| + | more_b = [] | ||
| + | | ||
| + | while not a.empty? | ||
| + | if(a[0] > pivot_a) | ||
| + | less.unshift(a[0]) | ||
| + | less_b.unshift(b[0]) | ||
| + | else | ||
| + | more.unshift(a[0]) | ||
| + | more_b.unshift(b[0]) | ||
| + | end | ||
| + | a.shift | ||
| + | b.shift | ||
| + | end | ||
| + | return wasDualQuicksort(less, | ||
| + | end | ||
| + | |||
| + | </ | ||
| + | |||
| + | ====== List Stride ====== | ||
| + | |||
| + | Returns a strided list starting from the element index '' | ||
| + | |||
| + | For example, given a list: | ||
| + | <code ruby> | ||
| + | a = [30, " | ||
| + | </ | ||
| + | |||
| + | the call: | ||
| + | <code ruby> | ||
| + | wasListStride(a, | ||
| + | </ | ||
| + | |||
| + | will return the list: | ||
| + | <code ruby> | ||
| + | [ 30, 20, 10, 9, 8, 7, 5 ] | ||
| + | </ | ||
| + | |||
| + | Given the same list, a call: | ||
| + | <code ruby> | ||
| + | wasListStride(a, | ||
| + | </ | ||
| + | |||
| + | will return the list: | ||
| + | <code ruby> | ||
| + | [ " | ||
| + | </ | ||
| + | |||
| + | <code ruby> | ||
| + | ########################################################################### | ||
| + | ## Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 ## | ||
| + | ########################################################################### | ||
| + | def wasListStride(list=[], | ||
| + | if start > 0 | ||
| + | list.shift | ||
| + | return wasListStride(list, | ||
| + | end | ||
| + | return [ list[0] ] + stride.step(list.size-1, | ||
| + | end | ||
| + | </ | ||
| + | |||
| + | ====== Get Symlink Path ====== | ||
| + | |||
| + | <code ruby> | ||
| + | require ' | ||
| + | Pathname.new('/ | ||
| + | </ | ||
| + | |||
| + | ====== Sort Files Into Directories ====== | ||
| + | |||
| + | The following program is meant to run in a directory containing unsorted files. The script creates directories '' | ||
| + | |||
| + | <code ruby> | ||
| + | # | ||
| + | ########################################################################### | ||
| + | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## | ||
| + | ########################################################################### | ||
| + | # Get all the files in the current directory. | ||
| + | entries = Dir.entries( | ||
| + | Dir.pwd | ||
| + | ).select( | ||
| + | & | ||
| + | |x| | ||
| + | !File.directory? | ||
| + | File.basename("# | ||
| + | } | ||
| + | ) | ||
| + | # Sort files in [0-9A-Z] directories. | ||
| + | (" | ||
| + | begin | ||
| + | if !Dir.exists? | ||
| + | Dir.mkdir(dir) | ||
| + | end | ||
| + | entries.select(& | ||
| + | File.rename(file, | ||
| + | end | ||
| + | rescue | ||
| + | puts "Could not move files to: " + dir | ||
| + | end | ||
| + | end | ||
| + | </ | ||
| + | |||
| + | An example application is a directory containing a bunch of archives which you want to sort into directories from '' | ||
For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.