Skip to content

Instantly share code, notes, and snippets.

View akostadinov's full-sized avatar

Aleksandar N. Kostadinov akostadinov

View GitHub Profile
@akostadinov
akostadinov / CSV-from-facebook-friends-export.rb
Last active January 22, 2021 18:11
Fix facebook broken exported UTF
require 'json'
require 'uri'
require 'csv'
# too bad we lack variable size lookbehind
bytes_re = /((?:\\\\)+|[^\\])(?:\\u[0-9a-f]{4})+/
friends_txt = File.read('friends.json').gsub(bytes_re) do |bad_unicode|
$1 + eval(%Q{"#{bad_unicode[$1.size..-1].gsub('\u00', '\x')}"}).to_json[1...-1]
end
@akostadinov
akostadinov / pretty_print_json.rb
Created January 26, 2021 11:59
Pretty print JSON-like exercise
INDENT_SIZE = 2
def pretty_jsonify(struct, indent=0)
case struct
when Array
"[\n" + indent_str("", indent+INDENT_SIZE) +
struct.map { |value|
pretty_jsonify(value, indent+INDENT_SIZE)
}.join(",\n#{indent_str('', indent+INDENT_SIZE)}") +
@akostadinov
akostadinov / amicable.rb
Created March 1, 2021 22:05
amicable numbers
# https://www.jdoodle.com/execute-ruby-online/
def amicable?(int1, int2)
sum_proper_divisors(int1) == int2 && sum_proper_divisors(int2) == int1
end
def sum_proper_divisors(num)
(1..num/2).select { |i|
num%i == 0
}.sum
@akostadinov
akostadinov / timebench.groovy
Last active May 10, 2021 04:26
Benchmark Java Thread-Safe Time Formatter Implementations
@Grapes([
@Grab(group='org.gperfutils', module='gbench', version='[0.4,)'),
@Grab(group='org.apache.commons', module='commons-lang3', version='[3.7,)'),
@Grab(group='joda-time', module='joda-time', version='[2.9.9,)')
])
/**
* Java 8 DateTimeFormatter
*/
import java.util.Date;
@akostadinov
akostadinov / xmr-stak.service
Last active May 14, 2021 07:35
Example xmr-stak systemd service unit file.
[Unit]
Description=xmr-stak miner
After=syslog.target network.target
[Service]
Type=simple
# interesting info about starting as regular user: https://bbs.archlinux.org/viewtopic.php?id=162297
User=username
LimitMEMLOCK=256M
EnvironmentFile=-/etc/sysconfig/xmr-stak
@akostadinov
akostadinov / prompt_include.sh
Last active May 14, 2021 11:44
bash red hat prompt on Fedora 34
function _shell_char {
if [ "$_EXIT_CODE" -eq 0 ]; then
#printf "%s" "$"
printf "$RED$shellchar$RESET"
else
# printf "$RED$FancyX$RESET"
printf "$RED$collision$RESET"
fi
}
@akostadinov
akostadinov / rails_dump_config.rb
Created August 19, 2021 11:18
represent Rails configuration as YAML
def dump_config(object = Rails.application.config.class.class_variable_get(:@@options))
case object
when Array
object.map { |v| dump_config(v) }
when Hash
Hash[ object.map { |key, value| [key, dump_config(value)] } ]
when TrueClass, FalseClass, Numeric
object
else
object.to_s
@akostadinov
akostadinov / udp.rb
Created February 18, 2019 19:07
Ruby UDP echo server and client
# echo server
Socket.udp_server_loop(4444) do |data, src|
src.reply data
end
# client
addr = Socket.sockaddr_in(4444, "localhost")
socket = Socket.new(:INET, :DGRAM)
begin
socket.send("hello\n", 0)
@akostadinov
akostadinov / rack_server.rb
Created November 18, 2021 14:18
Rack echo server
require "rack"
def pp(hash)
hash["HTTP_ACCEPT"].include?("text/html") ? pp_html(hash) : pp_plain(hash)
end
def pp_plain(hash)
hash.map {|key,value| "#{key}: #{value}"}.sort.join("\n")
end
@akostadinov
akostadinov / print_only_cuke_filter.rb
Created January 14, 2022 16:39
Cucumber list-only filter. Will only print list of feature files that were selected for testing. Needs to be registered as a last filter.
require 'cucumber/core/filter'
# put inside features/support/
class PrintOnlyCukeFilter < Cucumber::Core::Filter.new
def test_case(test_case)
announce
print_file_for(test_case)
end
private