Skip to content

Instantly share code, notes, and snippets.

View tkfm-yamaguchi's full-sized avatar

Takafumi Yamaguchi tkfm-yamaguchi

View GitHub Profile
@tkfm-yamaguchi
tkfm-yamaguchi / flatten_with_loop.rb
Last active December 28, 2018 00:17
flatten with loop (no recursion)
class Array
# flatten without recursion but loop
def myflatten!
i = 0
while i < self.size
unless self[i].is_a?(Array)
i += 1
next
end
@tkfm-yamaguchi
tkfm-yamaguchi / recover-file-removed.sh
Created December 27, 2018 00:13
recover removed file
sudo grep -i -a -B 100 -A 100 'string' /dev/sda1 > file.txt
# or
# sudo grep -F -a -C 100 'string' /dev/sda1 > file.txt
# ref:
# https://unix.stackexchange.com/questions/2677/recovering-accidentally-deleted-files
@tkfm-yamaguchi
tkfm-yamaguchi / apt-redis-issue.md
Created December 12, 2018 01:27
Can't upgrade/purge redis server on Debian/Ubuntu (apt adapting distros)

Situation

  • Upgrading redis-server is failed with apt upgrade.
  • apt purge redis-server is failed

The reason for the failure of both apt command above is something is timeout.

Solution (one of)

@tkfm-yamaguchi
tkfm-yamaguchi / all_coords_vtk_uniq.rb
Last active November 21, 2018 09:02
a ruby method to detect x-y coords uniquness of points in VTK file
# Detects whether the x/y coordinates of points in VTK are all different.
#
# @params {string} vtk path to VTK file
# @return {boolean}
def all_coords_uniq?(vtk)
File.open(vtk, "r") do |f|
points_count = 0
while line = f.gets.chomp
if line =~ /^POINTS/
points_count = line.split[1].to_i
@tkfm-yamaguchi
tkfm-yamaguchi / rubocop_emacs_formatter_expansion.rb
Created November 15, 2018 05:01
formatter for rubocop: output relative path with emacs style
module RuboCop
module Formatter
class EmacsStyleFormatter < BaseFormatter
def file_finished_with_relative_path(file, offenses)
path = Pathname(file).relative_path_from(Pathname.pwd).to_s
file_finished_without_relative_path(path, offenses)
end
alias file_finished_without_relative_path file_finished
@tkfm-yamaguchi
tkfm-yamaguchi / index.html
Created November 7, 2018 01:49
A prototyping starter set for React
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Prototping with React</title>
</head>
<body>
<div id="app"></div>
<script crossorigin src="https://unpkg.com/react/umd/react.development.js"></script>
@tkfm-yamaguchi
tkfm-yamaguchi / cb.py
Created October 22, 2018 07:10
CodingBat in Python
# https://codingbat.com/python/List-1
# https://codingbat.com/python/Logic-1
# https://codingbat.com/python/Logic-2
# https://codingbat.com/python/String-2
# https://codingbat.com/python/List-2
def cigar_party(cigars, is_weekend):
if is_weekend:
return cigars >= 40
else:
@tkfm-yamaguchi
tkfm-yamaguchi / string.rb
Last active October 16, 2018 07:42
ruby's string class extension conting the length of multi-byte string as 2
class String
# number of characters where the multi-byte string is counted by 2
def mbsize
chars.sum{|str| str.bytesize == 1 ? 1 : 2 }
end
def mbljust(num, char=" ")
return self if mbsize > num
self + char * (num - mbsize)
@tkfm-yamaguchi
tkfm-yamaguchi / ping_with_decimal_number.rb
Last active June 19, 2018 01:43
ping に10進数を指定したときの挙動についての確認スクリプト
# == ping プログラムの(?)仕様についての確認スクリプト
#
# $ ping 2915182161
#
# を実行すると,
#
# $ ping 173.194.38.81
#
# を実行したのと同じように扱われる. これは ping が引数をドット(`.`)を桁区切りとする 256 進数と同値であるため.
# 以下は, 2915182161 (10進数) が 173.194.38.81 (256進数) と同値であることを示すスクリプト.
@tkfm-yamaguchi
tkfm-yamaguchi / result.txt
Last active May 21, 2018 14:41
It would be nil (local variable) when a method is substituted by itself in Ruby
"method"
"a"
"local-variable"
nil