Skip to content

Instantly share code, notes, and snippets.

@mnbi
mnbi / picker.rb
Created October 31, 2020 01:28
Invoke a picker in program code
# How to invoke a picker in your code to select items interactively.
PICKER = 'fzf'
PICKER_OPTS = '-m' # allow multiple selection
def picker(prog, entries)
in_ary = entries.map(&:to_s)
IO.popen(prog, "r+") { |pipe|
in_ary.each {|e| pipe.puts(e)}
pipe.close_write
@mnbi
mnbi / exist_in_search_path.rb
Created October 6, 2020 03:13
predicate whether a file exists in the search path
#
# Which implementation do you prefer?
#
# 1.
def exist_in_search_path_1?(file, paths)
paths.split(":").each { |dir|
return true if FileTest.exist?(File.expand_path(file, dir))
}
false
@mnbi
mnbi / filesize.rb
Last active April 28, 2017 00:52
Print size of specified files.
#!/usr/bin/env ruby -w
# -*- coding: utf-8 -*-
# Print size of specified file(s) friendly and nicely for human beings
require 'optparse'
Version = '0.3.2'
Release = '2017-04-28'
@mnbi
mnbi / convertDate.m
Created November 23, 2010 18:19
convert a RFC3339 date string into a NSDate object
#import <Foundation/Foundation.h>
// convert a RFC3399 date (& time) into a NSDate object
// NOTE: This function ignores fractions of a second in the RFC3339
// representation.
NSDate *getDateObject(NSString *rfc3339)
{
// Date and Time representation in RFC3399:
// Pattern #1: "YYYY-MM-DDTHH:MM:SSZ"
// 1
@mnbi
mnbi / listprimes_sieve.rb
Created November 6, 2010 12:45
make a list of prime numbers using the sieve of Eratosthenes.
#!/opt/local/bin/ruby1.9 -w
# -*- coding: utf-8 -*-
# listprimes_sieve.rb: make a list of prime numbers using the sieve of Eratosthenes
start = ARGV.shift.to_i
start = 2 if start < 2
range = ARGV.shift.to_i
def sieve(start, range)
limit = start + range
@mnbi
mnbi / listprimes_simple.rb
Created November 6, 2010 12:35
make a list of prime numbers.
#!/opt/local/bin/ruby1.9 -w
# -*- coding: utf-8 -*-
# listprime.rb: make a list of prime numbers.
require 'prime'
def die(*x)
STDERR.puts x
exit 1
end
@mnbi
mnbi / listprimes_binsearch.rb
Created November 6, 2010 12:25
make a list of prime numbers, using the binary search algorithm to search the prime table.
#!/opt/local/bin/ruby1.9 -w
# -*- coding: utf-8 -*-
# listprimes_binsearch.rb: make a list of prime numbers.
def die(*x)
STDERR.puts x
exit 1
end
if ARGV.length < 2
@mnbi
mnbi / checkprimes.rb
Created November 5, 2010 13:20
check each prime in primes.txt
#!/opt/local/bin/ruby1.9 -w
# -*- coding: utf-8 -*-
# checkprimes.rb: check each prime in primes.txt
PRIMES_FILE = "primes.txt"
$primes = []
if File.exist?(PRIMES_FILE)
File.foreach(PRIMES_FILE) do |line|
$primes.push(line.to_i)
@mnbi
mnbi / listprimes.rb
Created November 5, 2010 13:18
make a list of prime numbers in the specified range.
#!/opt/local/bin/ruby1.9 -w
# -*- coding: utf-8 -*-
# listprimes.rb: make a list of prime numbers.
def die(*x)
STDERR.puts x
exit 1
end
if ARGV.length < 2
@mnbi
mnbi / request_history.py
Created November 1, 2010 10:25
a LIFO ring buffer
class RequestHistory(object):
def __init__(self, size=10):
self.size = size
self.modulus = self.size + 1
self.history = [''] * (self.modulus)
self.base = 0
self.top = 1
def is_empty(self):
return self.base == self.decrement(self.top)