Reverse List

###########################################################################
##  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

###########################################################################
##  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      ##
###########################################################################
def wasDualQuicksort(a = [], b = []) 
  raise("The sorted arrays must be of equal length.") unless(a.length == b.length)
  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, less_b) + [ pivot_a ] + [ pivot_b ] + wasDualQuicksort(more, more_b)  
end

List Stride

Returns a strided list starting from the element index start with a stride of stride. In other words, returns every stride-element starting from the index start.

For example, given a list:

a = [30, "g", 20, "c", 10, "a", 9, "f", 8, "d", 7, "e", 5, "b"]

the call:

wasListStride(a, 0, 2)

will return the list:

[ 30, 20, 10, 9, 8, 7, 5 ]

Given the same list, a call:

wasListStride(a, 1, 2)

will return the list:

[ "g", "c", "a", "f", "d", "e", "b" ]
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      ##
###########################################################################
def wasListStride(list=[], start=0, stride=0)
  if start > 0
    list.shift
    return wasListStride(list, start-=1, stride)
  end
  return [ list[0] ] + stride.step(list.size-1, stride).map { |i| list[i] }
end

Get Symlink Path

require 'pathname'
Pathname.new('/path/to/symlink').realpath

Sort Files Into Directories

The following program is meant to run in a directory containing unsorted files. The script creates directories 0 to 9 and A to Z and puts the files inside those directories by the first character of the file name.

#!/usr/bin/ruby
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3      ##
###########################################################################
# Get all the files in the current directory.
entries = Dir.entries(
    Dir.pwd
).select(
    &lambda{
        |x| 
        !File.directory?("#{x}") && 
            File.basename("#{$0}").casecmp(x) != 0 # omit us!
    }
)
# Sort files in [0-9A-Z] directories.
("0".."9").to_a.concat(('A'..'Z').to_a).each do |dir|
    begin
        if !Dir.exists?(dir)
            Dir.mkdir(dir)
        end
        entries.select(&lambda{|x| "#{x}".chars.first.casecmp(dir) == 0}).each do |file|
            File.rename(file, File.join(dir, 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 0 to 9 and A to Z.


fuss/ruby.txt ยท Last modified: 2017/02/22 18:30 (external edit)

Access website using Tor Access website using i2p


For the copyright, license, warranty and privacy terms for the usage of this website please see the license, privacy, data protection and copyright compliance as well as the plagiarism pages.