bar_1

contents_map

2018年12月21日金曜日

Thor はじめに Getting Started

始めるにあたり, thorについて知らなければならない2,3のことがある. 例えば, タスクをロードするためのファイル命名規則や, 通常はコマンドラインで thor コマンドによってあなたのスクリプトを呼び出すことである (これは他にも方法がある)
There are a few things that you need to know about thor to get started. For example, it uses a file name convention to load tasks, and you typically call your scripts via the thor command line, though this is not the only option.

ファイル名 File Names

thorタスクをひとつ作るには, 最初に, 拡張子 .thor をもつファイル (Thorfile というファイルでもよい) がひとつ必要となる. このファイルには, thorが提供する機能を含んだ任意の標準のRubyコードを, 入れることができる. thor コマンドライン・ツールが実行されると, もろもろの .thor ファイルを探し, ロードして, あなたが作成したタスク群が使用できるようになる.
To create a thor task, the first thing you need is a file ending with the .thor extension (a file named Thorfile works as well). This file can contain any standard ruby code, including the functionality provided by thor. When the thor command line tool is run, it will look for .thor files and load them up, making your tasks available.

シンプルな例 A Simple Example

test.thor というファイルを作って, 以下のコードを書く:
Create a file named test.thor and place the following code in it
class Test < Thor
  desc "example", "an example task"
  def example
    puts "I'm a thor task!"
  end
end
あなたは一旦これを作りさえすれば, コマンドラインから thor list を実行することで, 以下のような出力が得られる:
Once you have this created, you can run thor list from the command line and you should see the following output:
test
----
thor test:example  # an example task
さあ, コマンドラインから thor test:example を実行しよう. I'm a thor task! という出力が得られるはずだ.
Now run thor test:example from the command line. You should see I'm a thor task! output to the command line.

サンプルを深く理解する Breaking Down The Sample

このタスクには, 何が起きているかを理解するために試せる, 5,6個のパーツがある.
There are several parts of this task that can be examined to understand what is happening.
最初に, クラス名はタスクの名前空間として使われる. Thorは クラス名を名前空間に変えるために, もろもろのRuby標準命名規則を使う. 例えば, あなたがクラス名を FooBar に変えた場合, タスクはこのとき foo_bar:example としてリストされる.
First, the class name is used as the namespace for the task. Thor uses some standard ruby conventions to turn the class name into a namespace. If you change the class name to FooBar, for example, your task would now be listed as foo_bar:example.
次に, desc メソッドが使われているが, これはわれわれが作成しようとしているタスクを記述するのためのものだ. descメソッドには ふたつのパラメータ が ある. 第1のパラメータは 定義するタスク(メソッド)の名称を明示するためのもので, 任意のパラメータ群をそのメソッドに与えられる. 第2のパラメータは プレーンテキストで, このタスクが何をするものなのか説明するためのものだ.
Next, the desc method call is used to describe the task that we are creating. There are two parameters to the desc method. The first parameter should state the name of the task (the method) and any parameters that are provided to the method. The second option should be a plain text description of what this task does.
最後は, example メソッドそのものだ. Thorはこのメソッド名を実際のタスク名として使用する. それゆえ, exampleメソッドを widget にリネームした場合, タスクは test:widget としてリストされることになる.
Last, the example method itself. Thor uses this method name as the actual task name. Therefore, when you rename the example method to widget, you end up with a task listing of test:widget.

タスクのパラメータ Task Parameters

以下のthorタスクは, パラメータをひとつメソッドに加えることによって, タスクにパラメータをひとつ定義する:
The following thor task defines one parameter on the task by adding a parameter to the method:
class Test < Thor
  desc "example FILE", "an example task that does something with a file"
  def example(file)
    puts "You supplied the file: #{file}"
  end
end
descの第1パラメータを’FILE’という語を含むものにアップデートしたことに, 注意してほしい. 前述のように, タスクの記述にはタスク・パラメータを任意に含めてよい. これはエンドユーザが使いやすいようにヘルプ・テキストを提供するものだ.
Note that the description has been updated to include the word ‘FILE’ in the first parameter. As stated previously, you should include the name of any required task parameters in the task description. This is used to produce help text that is useful to the end user.
thor help test:example を実行してtest:exampleタスクのヘルプテキストを見てみると, 以下のような出力を見れるだろう:
Run thor help test:example to see the help text for the test:example task and you will see the following output:
Usage:
  thor test:example FILE

an example task that does something with a file
これはそのタスクにFILEパラメータが必要であることを示す.
This indicated the requirement of a FILE parameter for the task.
thor test:example my_file.rb でタスク自体を実行した場合, You supplied the file: my_file.rb と目にするはずだ.
Now when you run the task itself, with thor test:example my_file.rb you should see You supplied the file: my_file.rb.
もしファイル名を与えずにスクリプトを実行すれば, ひとつ与える必要があるという "example" was called incorrectly. Call as "thor test:example FILE". のメッセージを目にするだろう.
If you run the script without a file supplied, you’ll see a message saying you need to supply one: "example" was called incorrectly. Call as "thor test:example FILE".

タスクのオプション Options For A Task

Thorはまた, ひとつのタスクに対して, もろもろの名前付けしたオプションを与えることもできる. いくつかのメソッドがこれを行っており, より詳細については [[method options]] のWikiページを参照してほしい.
Thor also lets you provide named options for a task. There are several methods of doing this, and more detail can be found on the [[method options]] wiki page.
あなたのtest.thorファイルに以下を書いてみよ:
Put the following into your test.thor file:
class Test < Thor
  desc "example FILE", "an example task"
  method_option :delete, :aliases => "-d", :desc => "Delete the file after parsing it"
  def example(file)
    puts "You supplied the file: #{file}"
    delete_file = options[:delete]
    if delete_file
      puts "You specified that you would like to delete #{file}"
    else
      puts "You do not want to delete #{file}"
    end
  end
end
この例では, 5,6個のパラメータとともに method_option がひとつ指定されている. 第1のパラメータはオプションのフルネームだ. これはコマンドライン上で —オプションひとつに変換される. このケースでは --delete だ. :aliases オプションは, このオプションのために, ひとつかそれ以上の省略形のエイリアス群を提供する. このケースでは -d が省略形として提供される. 最後に :desc でこのオプションの説明を与えた.
In this example, a method_option has been supplied with several parameters. The first parameter is the full name of the option. this is translated into a — option on the command line. In this case, --delete. The :aliases option allows you to provide one or more short-hand aliases for the option. In this case, -d has been provided as a short-hand alias. And last, a description of the option has been provided.
thor help test:example を実行すると, 以下の出力がみれる:
Run thor help test:example and you will see the following output:
Usage:
  thor test:example FILE

Options:
  -d, [--delete=DELETE]  # Delete the file after parsing it

an example task
今, このタスクを —delete オプション有りでも無しでも実行できる.
Now you can run the task with or without the delete option.
thor test:example my_file.rb を実行すると, 以下のようになる:
Run thor test:example my_file.rb, you will see this output:
You supplied the file: my_file.rb
You do not want to delete my_file.rb
thor test:example my_file.rb --delete もしくは thor test:example my_file.rb -d で実行してみると:
Run thor test:example my_file.rb --delete or thor test:example my_file.rb -d and you will see:
You supplied the file: my_file.rb
You specified that you would like to delete my_file.rb
作成したタスク・メソッドひとつにつき, 多くのメソッド・オプションを必要なだけ指定できる. これらをメソッド名に対して積み上げていくだけで, タスク内に現れるだろう.
You can specify as many method options as you need, for a given task method. Just keep piling them on to the method name and they will show up in the task.

ハサミを持って突っ走る Running With Scissors

ここでの諸例は単純で不自然だったが, 今はなにかしら, 実際のthorタスクの機能を使ってみることができるだろう. thorのさまざまなオプションや能力のドキュメントについては, 他のwikiページ (訳注: githubにあるThorのWikiのこと. この訳文群のこと) もチェックしてみること.
Though the examples here are simple and contrived, you should be able to get started with some real functionality for your thor tasks, now. Be sure to check the other wiki pages for documentation on the various options and capabilities of thor.

2018年9月11日火曜日

中小企業向けサイバーセキュリティ対策の極意 (無料)


東京都産業労働局が, 『中小企業向けサイバーセキュリティ対策の極意』というPDF資料を配布している. 全193ページ, 5章からなる構成で, 無料である.
ページ下部のリンクからダウンロードできる.

2018年8月29日水曜日

"SafariBookmarksSyncAgentが予期しない理由で終了しました" の修正方法

いつのタイミングか忘れてしまったが, Safari を起動するとエラーメッセージが出るようになってしまってい, ブクマ登録もできなくなってしまった. これは ~/Library/Safari/Bookmarks.plist を作り直すことで, 直すことができる.
この記事では, 当該現象と原因, 修正方法を述べる.

2018年3月19日月曜日

国内・海外の証券会社とAPI公開状況 (2013年版)

下記は2013年に調べたものなので一部情報が古いかもしれない. 「海外」としたが, 時間的な制約から, 英語圏しか調査は行っていない.

2018年3月10日土曜日

debug/enbug

エンバグという単語について調べていたら, ジャーゴンとしては存在する単語であることがわかった (したがって, 和製英語というわけではない). 下記のY-Combinatorの掲示板で触れられていた:
Sorry to burst your bubble, but there exists enbug.org whose about page ([http://enbug.org/AboutThisSite](http://enbug.org/AboutThisSite)) says:

    The meaning of enbug is the opposite of the word debug, namely, to add errors or defects into software or hardware. 

That page was last edited on 2007-03-04.

2018年2月19日月曜日

macOS High SierraでKarabiner-Elementsだけで親指シフト入力環境を構築する方法

!!!!あたらしい記事が ここ にあります!!!!
Fortniteが動くかとおもいEl Capitan (10.11.x) から一気にHigh Sierraにアップグレードしてみたものの, グラボがないとダメですと言われてゲームが起動できなかった.
これまで私がSierra系に更新していなかった理由は, Karabinerが動かないと聞いていたからだ. こうなったらSierra以降向けに開発されているという Karabiner-Elements (KE) で, 無理矢理NICOLA配列での親指シフト入力環境を実現する しかない……というわけで, とりあえず動くものができたので公開します.
動作確認した環境は下記:
  • OS version - macOS High Sierra (10.13.3)
  • IME - Google 日本語入力 2.20.2700.1 (基本設定: ローマ字入力)
  • Karabiner-Elements version - 11.5.0
試してないけどSierraでもイケると思う.
インプリしたのは, とりあえず NICOLA配列 (JAWP) で, 右シフトを「変換」キー, 左シフトを「無変換」キーとしています.
現状のこのNICOLA配列の実装の不具合としては:
  • けっこう前もって (よっこらしょという感じで) 親指シフトキーを押す必要がある
  • ときどきシフト状況が正確に把握されないことがある
という点が挙げられます. もっとうまい書き方があるかもしれない.

使い方

Karabiner-Elements (KE) をインストールしてから,
上記の2つのjsonファイルをダウンロードして, ディレクトリ: $HOME/.config/karabiner/assets/complex_modifications/ に格納してください.
次に, Karabiner-Elementsを起動してから Preferences... を開き, Complex Modifications タブを選択します. 下の方にある Add rule ボタンを押すと, 設定の一覧が出てきます.
この一覧の下の方に, 「無変換, 変換キーでIME Off, On」, 「親指シフト入力のためのNICOLA配列」があるので, Enableして下さい.

おおまかな仕組み

シフトごとにフラグとなる変数: Oyayubi-R, Oyayubi-L, Oyayubi-Any (右か左のどちらかが押されたとき用) を用意 (set_variable) して, 押下されたらセット, 離されたらリセットしています. この処理の記述をしているのが, muhenkan_henkan_ime.json です.
フラグの状態とIMEの状態に応じてmanipulateの処理が決まります. こちらは nicola_genenerated.json で記述しています.
ファイル名から察しがつくように, いちいちキー一つ一つの設定を書いていくのは面倒なので, 設定のためのjsonジェネレータをRubyで書いた――のだけど, こういうものがあると後から知った:

Conclusion

macOS High Serraで, Karabiner-Elementsだけで親指シフト入力を可能にした.

References

実装するにあたり, 以下のドキュメントが役に立ちました.

2017年12月10日日曜日

つぶやき日記2017-12-10

  • Mac環境のPythonパッケージであるANACONDAをインストールしたら, PATHが変わってiconvがうまく動かなくなって (conversion from CP943 unsupported) しまっていたことに気づいた. -f CP943というオプションが使えない /Users/user_name/anaconda/bin ではなく, MacOSがシステム標準で用意している/usr/bin/iconvを使用するようにした.
  • k-db.comが今年いっぱいでサービス停止とのこと.

2017年11月19日日曜日

[Ruby]モジュールに継承/派生っぽいことをさせてみる試み

組み込みライブラリもしくは既存のクラスclassやモジュールmoduleを, 特定の用途向けに一部だけ改変したいことがある. その際, できればモンキーパッチはしたくない.
このような場合, クラスならば class DerivedKlass < OriginalKlass として継承を使える. 派生クラスを作ることで, 元のクラスのコピーを作り, それを改変すればいい.
ではモジュールの場合は, どうしたらよいだろう? ここでは, Mathを取り上げて考えてみる.
最初に, include/extend/refineという既存の方法の検討をおこない問題点を明らかにする. 次に, これらの問題点を解決する Module#module_compose の導入を提案する.

2017年11月16日木曜日

Rubyの実験: Moduleのprivateインスタンスメソッド

Moduleのprivateインスタンスメソッド, 具体的にはModule::module_functionのふるまいで, 腑に落ちない点があった. 派生先のClassクラス (ひいては一般に定義されたクラス) からなぜ使えないのか? ということで, 実験した. 結論的には 仕様だった, という落ちでした……w

2017年11月8日水曜日

Rubyの実験: Object/Class/Module/Kernelにメソッドを追加するとどうなるか?

Object/Class/Module/Kernelにメソッドを追加すると, これらや一般のクラス, モジュールやインスタンスに対してどのような影響があるか, 調べるコードを書いてみた. 追加するメソッドはインスタンスメソッドと特異メソッドで, 両方publicの可視属性です.