This is an old revision of the document!


Reverse List

##########################################################
## Wizardry and Steamworks (c) 2013,  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

##########################################################
## Wizardry and Steamworks (c) 2013,  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" ]
##########################################################
## Wizardry and Steamworks (c) 2013,  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

fuss/ruby.1420781647.txt.bz2 ยท Last modified: 2015/04/15 17:00 (external edit)

Wizardry and Steamworks

© 2025 Wizardry and Steamworks

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.