Movatterモバイル変換


[0]ホーム

URL:


rubyco(るびこ)の日記

この広告は、90日以上更新していないブログに表示しています。

String#stripは文字列前後の空白を削除します

  • String#stripは文字列前後の空白を削除します。
"  Hello, world!".strip#=> "Hello, world!"
  • String#strip!は文字列前後の空白を削除します(破壊的メソッド)。
>>"   Hello, world!".strip=>"Hello, world!">>"\t\t\tHello, world!\n\n\n".strip=>"Hello, world!">> s ="   Hello, world!"=>"   Hello, world!">> s=>"   Hello, world!">> s.strip=>"Hello, world!">> s=>"   Hello, world!"# String#strip は非破壊的>> s.strip!=>"Hello, world!">> s=>"Hello, world!"# String#strip! は破壊的

ハッシュの値が存在しないときの値(デフォルト値)を設定する

  • ハッシュの値がまだ存在しなかったら0を代入し、あったら数を足し込んでいこうと思います。
h =Hash.newifnot h[:key]  h[:key] =0endh[:key] +=123
  • Hash#key?メソッドを使うべきかしら。
h =Hash.newifnot h.key?:key  h[:key] =0endh[:key] +=123
  • ハッシュを作るときにHash.new(0)でデフォルト値0を与えておくと「存在しなかったら」というif文が不要になります。
h =Hash.new(0)h[:key] +=123
  • 同じように空の配列をデフォルトにすることも。
h =Hash.new([])h[:key] <<:value
  • ビットパターンも。
h =Hash.new(0)h[:key] |=0b1011

パスワードを作成するRubyスクリプト

  • パスワードを作成するRubyスクリプト makepass.rb を作りました。無保証ですので、使いたい方は自己責任で。
# makepass.rbrequire'io/console'require'digest/sha2'require'base64'require'win32/clipboard'Win32::Clipboard.set_data('')STDIN.noechodo |io|  puts"Hit many keys randomly."  s = io.getswhile s.length <100    puts"I need more random keys."    s += io.getsend  d =Digest::SHA512.digest(s)  e =Base64.encode64(d)  e.gsub!(/[\+\/=\n]/,'')  puts"Thanks.  I've saved a password-like string in your clipboard."Win32::Clipboard.set_data(e)end
  • 使っている様子
C:\> ruby -vruby 1.9.3p374 (2013-01-15) [i386-mingw32]C:\> gem install win32-clipboardFetching: win32-api-1.4.8-x86-mingw32.gem (100%)Fetching: windows-api-0.4.2.gem (100%)Fetching: windows-pr-1.2.2.gem (100%)Fetching: win32-clipboard-0.5.2.gem (100%)Successfully installed win32-api-1.4.8-x86-mingw32Successfully installed windows-api-0.4.2Successfully installed windows-pr-1.2.2Successfully installed win32-clipboard-0.5.24 gems installedInstalling ri documentation for win32-api-1.4.8-x86-mingw32...Installing ri documentation for windows-api-0.4.2...Installing ri documentation for windows-pr-1.2.2...Installing ri documentation for win32-clipboard-0.5.2...Installing RDoc documentation for win32-api-1.4.8-x86-mingw32...Installing RDoc documentation for windows-api-0.4.2...Installing RDoc documentation for windows-pr-1.2.2...Installing RDoc documentation for win32-clipboard-0.5.2...C:\> ruby makepass.rbHit many keys randomly.     ←ここでキーボードを乱打し、最後にENTERキーを打つ。I need more random keys.    ←キー不足のときにはこういう表示になる。Thanks.  I've saved a password-like string in your clipboard.この時点でクリップボードに、4GXt2VP9lpievshQmAVYFOJtXqQ26YadHFei1HYRyYTnNH8EPvIETEg6rjTr6DssOrPSArECcCCqrqfBsqAのようなパスワードっぽい文字列が入っているので適当に使う。
  • 解説
    • ユーザがキーボードを乱打した文字列を予測困難な文字列として用いる。
    • ある程度の長さがあったほうがいいので100文字くらいにしている(適当)。
    • その文字列を暗号学的に強いメッセージダイジェスト関数SHA-256を用いてハッシュ値を求める。
    • ハッシュ値Base64エンコードする。
    • 改行と記号(+, /, =)を取り除く(入れたい方は好きに)。
    • できた文字列をクリップボードにセットする。
  • 注意

WindowsでRuby 1.8.6 → 1.9.3 へ。

最近Rubyをアップデートしてなかったので、この機会に1.9へ。

C:\>ruby -vruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
C:\>ruby -vruby 1.9.3p125 (2012-02-16) [i386-mswin32_100]

はい、できました。

『数学ガール/ガロア理論』をLaTeXで書いているときに使っていたRubyスクリプト

結城浩(id:hyuki)さんが『数学ガール/ガロア理論』LaTeXで書いているときに使っていたRubyスクリプトです。
Windowsで動作することを仮定しています。
やっていることは、

というものです。ほとんどバッチファイル代わりですね。
Acrobatが共有禁止で握っているPDFに上書きするために、途中でAcrobat.exeを殺しています。乱暴な…。

require"fileutils"PLATEX_OPTION =''TMP_DIR ='\\tmp'DROPBOX_DIR ='C:\\My Dropbox'PDF_NUMBER ='5'ifARGV.length ==0abort"Usage: texpdf.rb filename[.tex] [a5|a4] [once|twice]"endfrom =ARGV[0].gsub(/\.tex$/,"")ifARGV.length >=2ifARGV[1] =='a5' ||ARGV[1] =='a4'    page_size =ARGV[1]else    page_size ='a4'endendifARGV.length >=3ifARGV[2] =='once'MAKE_INDEX =falseelseMAKE_INDEX =trueendendtime =Time.now.strftime("%Y-%m-%d-%H%M%S")to_dir ="#{TMP_DIR}\\_#{time}"texname ="#{from}.tex"dvifile ="#{from}.dvi"pdffile ="#{from}.pdf"system("mkdir#{to_dir}")system("xcopy /Y /s *.*#{to_dir}")Dir.chdir("#{to_dir}")puts"Create#{dvifile}."system("platex#{PLATEX_OPTION}#{texname}")abort"pLaTeX error."unless$? ==0puts"Kill Acrobat."system("taskkill /IM Acrobat.exe")ifMAKE_INDEX  puts"Make index."  system("makeindex#{from}")  system("mendex#{from}")  system("platex#{PLATEX_OPTION}#{texname}")abort"pLaTeX error."unless$? ==0endabort"#{dvifile} is not found."unlessFile.exist?(dvifile)puts"Create#{pdffile}."system("dvipdfmx -d#{PDF_NUMBER} -p#{page_size}#{dvifile}")abort"#{pdffile} is not found."unlessFile.exist?(pdffile)puts"Copy to dropbox."system("xcopy /Y#{pdffile} \"#{DROPBOX_DIR}\"")puts"Show#{pdffile}."system("start#{pdffile}")

Windows XPでmrubyをビルドして動かす

祝! 軽量Rubyが公開を参考にさせていただいて、Windows XPでmrubyをビルドして動かしてみました。

  • mrubyの入手。
  • cygwinのインストール。
  • mrubyのビルド
    • cygwin Terminalを動かします。
    • Makefileがあるところでmakeします。でもbisonがないと言われてしまいました。Cygwinでインストール不足。
    • もう一度cygwinのsetup.exeを動かして、bisonを検索してアップデート。m4も入れますよと言われる。はいはいわかりました。
    • makeしたらしばらくしてbinの下にexeができました。
$ ls binmrbc.exe  mruby.exe  mrubysample.exe
$ cat hello.rbprint "Hello!\n"
    • で、実行。
$ ./bin/mruby hello.rbHello!
  • 動きました。

(ご指摘を受けて、s/mRuby/mruby/しました)

mrubyのソースコードを眺める

すでにGitがインストールされているとして。

C:\> mkdir mrubyC:\> cd mrubyC:\mruby> git clone https://github.com/mruby/mruby.gitCloning into mruby...remote: Counting objects: 462, done.remote: Compressing objects: 100% (247/247), done.remote: Total 462 (delta 222), reused 448 (delta 211)Receiving objects: 100% (462/462), 560.79 KiB | 229 KiB/s, done.Resolving deltas: 100% (222/222), done.C:\mruby> cd mruby

あとは好きに眺める。

(るびこさんのエントリを書くのは久しぶりですね)

検索

引用をストックしました

引用するにはまずログインしてください

引用をストックできませんでした。再度お試しください

限定公開記事のため引用できません。

読者です読者をやめる読者になる読者になる

[8]ページ先頭

©2009-2025 Movatter.jp