Skip to content

Instantly share code, notes, and snippets.

View zverok's full-sized avatar

Victor Shepelev zverok

View GitHub Profile

Inspired by Lucian Ghinda's article about code review/refactoring with AI, just an experimenting on looking at the code the way I usually do that.

It is not an attempt to demonstrate "how everybody should write code," rather a way of sharing how I approach code style and refatoring. (I.e. not to communicate "you should do this," but just "here are some techniques and considerations I use, maybe you'll find it interesting").

So...

Here's the original code:

class Parser

Some small additions/comments to an article by Kevin Newton "Ruby operators"

% - The modulo operator. This is usually only found on numeric types.

And Ranges :)

(1..30) % 5
# => ((1..30).%(5)) -- an ArithmeticSequence
@zverok
zverok / unmunch.py
Created May 9, 2021 13:01
"Unmunch" (linearize) word list from Hunspell dictionary with the help of Spylls library
# This is "unmunching" script for Hunspell dictionaries, based on Spylls (full Python port of Hunspell):
# https://github.com/zverok/spylls
#
# "Unmunching" (Hunspell's term) is a process of turning affix-compressed dictionary into plain list
# of all language's words. E.g. for English, in the dictionary we have "spell/JSMDRZG" (stem + flags
# declaring what suffixes and prefixes it might have), and we can run this script:
#
# python unmunch.py path/to/en_US spell
#
# Which will produce this list:
@zverok
zverok / err1.md
Last active January 19, 2020 12:12
Example of how Ruby's error formatting breaks Markdown

(Just quick copy-paste into the dialog/GitHub issue.)

Ugh, do you know why am I having undefined local variable or method foo' for main:Object?.. I expected it to work for Logger`.

Source:

Ugh, do you know why am I having undefined local variable or method `foo' for main:Object?.. I expected it to work for `Logger`.
# Just porting examples of .unfold usability from
# http://weblog.raganwald.com/2007/11/really-simple-anamorphisms-in-ruby.html
# http://weblog.raganwald.com/2007/11/really-useful-anamorphisms-in-ruby.html
# ...to Ruby 2.7+ Enumerator.produce (and numbered block args):
# Simple examples: http://weblog.raganwald.com/2007/11/really-simple-anamorphisms-in-ruby.html
# 10.unfold { |n| n-1 unless n == 1 }.inspect => [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
p Enumerator.produce(10) { _1 - 1 }.take_while { _1 >= 1 }
#=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
module Refinements
refine Object do
def derp
yield self
end
end
end
module A
using Refinements
require 'infoboxer'
page = Infoboxer.wikipedia.get('List of urban areas by population')
res = page
.sections(/Urban areas/).tables.first
.rows[1..-1].map { |r|
{
city: r.cells[2].to_s,
country: r.cells.templates.first.name, # What you see as Japan on page, in page code looks like {{JPN}}
# Small thing for installing gems on-the-fly.
# Useful for "self-contained" scripts, too small to be
# distributed with own Gemfile & README and stuff.
#
# Usage:
#
# gimme_gem 'sequel' # Checks OR installs the gem
# Sequel.connect 'mysql://....'
#
# All credits for managing RubyGems code goes to author
# It is show-the-point demo for my article
# "On DataFrame datatype in Ruby" http://zverok.github.io/blog/2016-01-10-dataframe.html
require 'good_data_frame' # `require': cannot load such file -- good_data_frame (LoadError)
# Initialization (with default index): hashes of column: values
# Values of each column are homogenous
# First and most important thing is "what columns is"
table = GDF.new(
manager: ['Tom', 'Jerry', 'Magda'],
require 'pp'
class Path < Struct.new(:start_node, :end_node, :color, :type)
def to_s
[start_node, end_node, color, type].join(' ')
end
end
class Test