Differences

This shows you the differences between two versions of the page.


Previous revision
fuss:ruby [2022/04/19 08:28] (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
 +
 +</code>
 +
 +====== Map Preserving Sort using Quicksort for Integers ======
 +
 +<code ruby>
 +###########################################################################
 +##  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
 +
 +</code>
 +
 +====== 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:
 +<code ruby>
 +a = [30, "g", 20, "c", 10, "a", 9, "f", 8, "d", 7, "e", 5, "b"]
 +</code>
 +
 +the call:
 +<code ruby>
 +wasListStride(a, 0, 2)
 +</code>
 +
 +will return the list:
 +<code ruby>
 +[ 30, 20, 10, 9, 8, 7, 5 ]
 +</code>
 +
 +Given the same list, a call:
 +<code ruby>
 +wasListStride(a, 1, 2)
 +</code>
 +
 +will return the list:
 +<code ruby>
 +[ "g", "c", "a", "f", "d", "e", "b" ]
 +</code>
 +
 +<code ruby>
 +###########################################################################
 +##  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
 +</code>
 +
 +====== Get Symlink Path ======
 +
 +<code ruby>
 +require 'pathname'
 +Pathname.new('/path/to/symlink').realpath
 +</code>
 +
 +====== 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.
 +
 +<code ruby>
 +#!/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
 +</code>
 +
 +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: 2022/04/19 08:28 by 127.0.0.1

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.