Skip to content

Instantly share code, notes, and snippets.

View masasakano's full-sized avatar

Masa Sakano masasakano

View GitHub Profile
@masasakano
masasakano / country_town_dynamic_dropdown.html
Last active July 6, 2021 15:08
Dynamic dropdown menu for country-town. Town menu hidden first. It'll appear, but including optgroup country name.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<a href="https://jsfiddle.net/masa_alpin/z1sjekvt/35/">jsfiddle</a>
<!-- country-town working with town hidden mostly4 -->
<!-- country-town dynamic dropdown menu. Town menu hidden first. It'll appear, but including optgroup country name, with tidied-up code. -->
<form action="/persons" accept-charset="UTF-8" method="post">
@masasakano
masasakano / zenkaku_to_ascii.rb
Created July 4, 2021 00:05
A Ruby helper method to convert CJK (Japanese) Zenkaku to ASCII
# coding: utf-8
# Nkf is a built-in Gem/library up to Ruby 3.0 at least.
#
# The following method is a wrapper for NKF.
#
# NKF (Nihon-go Kanji Filter) is an old library since 1990s. But I find it is
# the most convenient library still in 2021 to handle text with
# some nasty Japanese character code (n.b., the "proper" Japanese character code
# sets are fine, but unfortunately the character code sets popularalized
@masasakano
masasakano / rails_undestroyable_children.rb
Created June 25, 2021 12:28
Ruby-on-Rails model helper methods to get dependent children of a record, maybe only those that are not cascade-destroyed.
module ApplicationHelper
# to check whether a record has any dependent children
#
# @see https://stackoverflow.com/a/68129947/3577922
#
def has_children?
## This would be simpler though may initiate more SQL calls:
# self.class.reflect_on_all_associations.map{ |a| self.send(a.name).any? }.any?
self.class.reflect_on_all_associations.each{ |a| return true if self.send(a.name).any? }
@masasakano
masasakano / fortran90.memo.202103.ja.md
Last active March 31, 2021 21:37
Memorandum about Fortran 90 (or Fortran 2008) and gfortran as of 2021 March

Memo: Fortran90

Fortran90

Fortran は、Fortran 77 (古くはFortran 66) と Fortran 90 とに大別される。Fortran 90 は、さらに、Fortran 90, 95, 2000, 2003, 2008 などに分類され、後者になればなるほど、新しい仕様や文法が加わったり、古い文法が非推奨になったりしている。とはいえ、77と90の間の違いが、他のどのバージョン間の違いよりも圧倒的に大きい。したがって、Fortran90以降を総称してFortran90と呼ばれることが少なくない。

加えて、(Fortranコンパイラーの)ベンダーごとで、微妙な違いはある。有名なのは、Fortran 77 の仕様「DO 〜 (行番号必須)CONTINUE」で、これについては相当な早期に大半のベンダーが「DO 〜 (行番号なし)END DO」という拡張書式を認めたようだ。あるいは、ORACLE Fortranでは、unsigned integerが定められているが、これはFortran 2008の仕様にも定められていない、ORACLEの独自拡張だ。別のポイントとして、UNIXシステムとのやり取りにおいて、Fortranプロセスからシステムに返り値をわたして終了するcall EXIT(1)が使えるFortranコンパイラーが多い様子だが、これはFortranの仕様書には定められていないらしい(参考: gfortran:EXIT)。

このベンダーごとの相違は時に問題になり得る。昔動いていたコードが20年後には動かなくなったりする。Fortranの仕様では、私の知る限りbackward compatibilityは整っているので、昔動いていたコードが動かなくなったのであれば、それは、

@masasakano
masasakano / user_columns_confirmable.rake
Last active October 20, 2020 19:35
Check/Update the null columns for Rails/Devise-confirmable-related columns in Table users
# Check/Update the null columns for Rails/Devise-confirmable-related columns in Table users
#
# In Devise for Ruby on Rails, if there are users before you activate confirmable,
# and if the allowance time before confirmation is zero (Default(!)),
# then the existing users cannot log in any more (as of October 2020).
# It is because the confirmable-related columns for those users are nil
# (though I think it should be regarded as a bug of Devise-confirmable).
#
# This Rake task deals with such situation; it checks or updates
# the relevant columns of the table "users" in the DB for those who cannot
@masasakano
masasakano / japanese_char_conversion.rb
Last active November 14, 2019 18:08
Ruby sample code to highlight the difference among Japanese-character conversion methods
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# Sample code for my answer:
# https://stackoverflow.com/questions/58829740/how-do-i-filter-out-invisible-characters-without-affecting-japanese-character-se/58863231#58863231
require 'nkf'
begin
require 'iconv' # https://rubygems.org/gems/iconv/
rescue LoadError
@masasakano
masasakano / ruby_minitest_executable_template.rb
Last active November 6, 2019 17:28
Ruby template for unit-testing an executable with minitest
# -*- encoding: utf-8 -*-
# Template file for unit-testing an executable with Ruby minitest.
#
# @author Masa Sakano (Wise Babel Ltd)
require 'open3' # To obtain status, STDOUT, STDERR
require 'tempfile' # if using Tempfile
gem "minitest"
@masasakano
masasakano / masa_template.gemspec
Last active November 7, 2019 08:51
Masa's template gemspec file, implementing various mechanism to prevent human-errors
# -*- encoding: utf-8 -*-
require 'rake'
require 'date'
Gem::Specification.new do |s|
s.name = 'MY_GEM_NAME'.sub(/.*/){|c| (c == File.basename(Dir.pwd)) ? c : raise("ERROR: s.name=(#{c}) in gemspec seems wrong!")}
s.version = "0.1.0".sub(/.*/){|c| fs = Dir.glob('changelog{,.*}', File::FNM_CASEFOLD); raise('More than one ChangeLog exist!') if fs.size > 1; warn("WARNING: Version(s.version=#{c}) already exists in #{fs[0]} - ok?") if fs.size == 1 && !IO.readlines(fs[0]).grep(/^\(Version: #{Regexp.quote c}\)$/).empty? ; c } # n.b., In macOS, changelog and ChangeLog are identical in default.
# s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.bindir = 'bin'
@masasakano
masasakano / suppress_stderr.rb
Last active November 5, 2019 19:30
Suppress STDERR in the given block
# Suppress $stderr in the given block
#
# @author Masa Sakano, under Licence of CC BY-SA 4.0
#
# @example Simply suppressing $stderr (but not STDERR)
# suppress_stderr{ warn "Not displayed."; a=5 } #=> 5
#
# @example Backup-ping STDERR in a block, suppressing $stderr (and warn)
# suppress_stderr('/tmp/backup.log'){ warn "Not displayed."; a=5 } #=> 5
#
@masasakano
masasakano / rails_method_cheat_sheets.md
Created October 15, 2018 21:16
Cheat sheets of Ruby-on-Rails methods (Model and Relation; Rails 5.2)

% Ruby-on-Rails method cheat sheets

Optimised for pandoc flavour, but works more or less on Github flavour.

Relations instance methods and Model class methods

The table summarises the instance methods of Relations, which can be used as class methods of Methods, and their returned values (self means a Relation instance, even when it is run as a class method of a Model). Several instance methods of Model instances that do similar operations are also included.