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.new if not h[:key] h[:key] = 0 end h[:key] += 123
- Hash#key?ã¡ã½ããã使ãã¹ããããã
h = Hash.new if not h.key? :key h[:key] = 0 end h[: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.rb require 'io/console' require 'digest/sha2' require 'base64' require 'win32/clipboard' Win32::Clipboard.set_data('') STDIN.noecho do |io| puts "Hit many keys randomly." s = io.gets while s.length < 100 puts "I need more random keys." s += io.gets end 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 -v ruby 1.9.3p374 (2013-01-15) [i386-mingw32] C:\> gem install win32-clipboard Fetching: 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-mingw32 Successfully installed windows-api-0.4.2 Successfully installed windows-pr-1.2.2 Successfully installed win32-clipboard-0.5.2 4 gems installed Installing 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.rb Hit 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ã§ã¨ã³ã³ã¼ãããã
- æ¹è¡ã¨è¨å·(+, /, =)ãåãé¤ãï¼å ¥ãããæ¹ã¯å¥½ãã«ï¼ã
- ã§ããæååãã¯ãªãããã¼ãã«ã»ããããã
- 注æ
- ä¹±ææã«ã¨ã³ã¼ããã¯ãé²ãããã®IO#noechoã使ãããã«Ruby 1.9.3ã使ã£ã¦ããã
- ã¯ãªãããã¼ãã«ã»ããããããã«win32-clipboardã使ã£ã¦ããã®ã§Windowsä¾åã«ãªã£ã¦ããã
- Ruby 1.9.3 for Windowsã¯ãã¨ãã° http://rubyinstaller.org/ ããã¤ã³ã¹ãã¼ã«ã§ããã
Windowsã§Ruby 1.8.6 â 1.9.3 ã¸ã
æè¿Rubyãã¢ãããã¼ããã¦ãªãã£ãã®ã§ããã®æ©ä¼ã«1.9ã¸ã
C:\>ruby -v ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
- Rubyãæ¤ç´¢ãã
- http://www.ruby-lang.org/ja/downloads/ ããã
- http://www.artonx.org/data/asr/ ã¸è¡ãã
- Ruby-1.9.3-p125 Microsoft Installer Package(2012-02-16) ããã¦ã³ãã¼ãã
- Ruby-1.9.3.msiãå®è¡ã
- ã¤ã³ã¹ãã¼ã«å 㯠C:\ruby
C:\>ruby -v ruby 1.9.3p125 (2012-02-16) [i386-mswin32_100]
ã¯ããã§ãã¾ããã
ãæ°å¦ã¬ã¼ã«ï¼ã¬ãã¢çè«ããLaTeXã§æ¸ãã¦ããã¨ãã«ä½¿ã£ã¦ããRubyã¹ã¯ãªãã
çµå浩(id:hyuki)ããããæ°å¦ã¬ã¼ã«ï¼ã¬ãã¢çè«ããLaTeXã§æ¸ãã¦ããã¨ãã«ä½¿ã£ã¦ããRubyã¹ã¯ãªããã§ãã
Windowsã§åä½ãããã¨ãä»®å®ãã¦ãã¾ãã
ãã£ã¦ãããã¨ã¯ã
- ä¸æçãªãã£ã¬ã¯ããªãä½æãã
- ã«ã¬ã³ããã£ã¬ã¯ããªã®ä¸ã®ãã¡ã¤ã«ãå ¨é¨ããã«ã³ãã¼ãã
- ããã§LaTeXãåããã
- å ´åã«ãã£ã¦ã¯makeindexã¨mendexãåããï¼ã³ãã³ãã©ã¤ã³ã§ twice ã¨æ¸ãï¼ã
- PDFãä½ãã
- Dropboxã«ã³ãã¼ãã¦ããã
- Acrobatã§ãã¬ãã¥ã¼ããã
ã¨ãããã®ã§ããã»ã¨ãã©ããããã¡ã¤ã«ä»£ããã§ããã
Acrobatãå
±æç¦æ¢ã§æ¡ã£ã¦ããPDFã«ä¸æ¸ãããããã«ãéä¸ã§Acrobat.exeã殺ãã¦ãã¾ããä¹±æ´ãªâ¦ã
require "fileutils" PLATEX_OPTION = '' TMP_DIR = '\\tmp' DROPBOX_DIR = 'C:\\My Dropbox' PDF_NUMBER = '5' if ARGV.length == 0 abort "Usage: texpdf.rb filename[.tex] [a5|a4] [once|twice]" end from = ARGV[0].gsub(/\.tex$/, "") if ARGV.length >= 2 if ARGV[1] == 'a5' || ARGV[1] == 'a4' page_size = ARGV[1] else page_size = 'a4' end end if ARGV.length >= 3 if ARGV[2] == 'once' MAKE_INDEX = false else MAKE_INDEX = true end end time = 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 $? == 0 puts "Kill Acrobat." system("taskkill /IM Acrobat.exe") if MAKE_INDEX puts "Make index." system("makeindex #{from}") system("mendex #{from}") system("platex #{PLATEX_OPTION} #{texname}") abort "pLaTeX error." unless $? == 0 end abort "#{dvifile} is not found." unless File.exist?(dvifile) puts "Create #{pdffile}." system("dvipdfmx -d #{PDF_NUMBER} -p #{page_size} #{dvifile}") abort "#{pdffile} is not found." unless File.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ã®å
¥æã
- gitã¯Windowsä¸ã§ã¤ã³ã¹ãã¼ã«ãã¦ããã¨ãã¾ãã
- git clone https://github.com/mruby/mruby.git ã§ã½ã¼ã¹ã³ã¼ããå¾ã¾ãã
- cygwinã®ã¤ã³ã¹ãã¼ã«ã
- http://cygwin.com/install.html
- setup.exeãåãããæ示ã«å¾ããDefaultã§ã¤ã³ã¹ãã¼ã«ãã¾ããã
- mrubyã®ãã«ã
- cygwin Terminalãåããã¾ãã
- Makefileãããã¨ããã§makeãã¾ããã§ãbisonããªãã¨è¨ããã¦ãã¾ãã¾ãããCygwinã§ã¤ã³ã¹ãã¼ã«ä¸è¶³ã
- ããä¸åº¦cygwinã®setup.exeãåããã¦ãbisonãæ¤ç´¢ãã¦ã¢ãããã¼ããm4ãå ¥ãã¾ããã¨è¨ããããã¯ãã¯ããããã¾ããã
- makeããããã°ãããã¦binã®ä¸ã«exeãã§ãã¾ããã
$ ls bin mrbc.exe mruby.exe mrubysample.exe
- ã¤ã³ã¿ããªã¿ãåããã
- hello.rbãä½ããããã¯Windowså´ã§ä½ãã¾ããã
$ cat hello.rb print "Hello!\n"
-
- ã§ãå®è¡ã
$ ./bin/mruby hello.rb Hello!
- åãã¾ããã
ï¼ãææãåãã¦ãs/mRuby/mruby/ãã¾ããï¼
mrubyã®ã½ã¼ã¹ã³ã¼ããçºãã
ãã§ã«Gitãã¤ã³ã¹ãã¼ã«ããã¦ããã¨ãã¦ã
C:\> mkdir mruby C:\> cd mruby C:\mruby> git clone https://github.com/mruby/mruby.git Cloning 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
ãã¨ã¯å¥½ãã«çºããã
ï¼ãã³ãããã®ã¨ã³ããªãæ¸ãã®ã¯ä¹ ãã¶ãã§ããï¼