[DMY] [VB] [VB6] [Python] [C#] [Android] [電子工作] [個人メモ] [仕事メモ] [アイデア] [TstLink] [CE] [LTSA]
[自動化] [Qt] [OCaml] [3Dプリンタ] [TOOLS] [RAA] [Forge]
[機能別索引] [逆引きRuby] [マニュアル] [るびま] [標準ライブラリ] [るりまサーチ] [Try! Ruby] [PRaggerまとめ] [ピジョン・ブラッド]
[Rubyコーディング規約] [プログラミングのオキテ] [Rubyist SNS] [TOOLBIS]
[RubyネットワークProg] [druby] [コードなにがし] [RDocテンプレ] [Ruby/Tkサンプル]
[WAVE] [Xperia] [github] [twitterなど] [EA] [マクロ]
2014-08-25
RubyでMsgBox,InputBox,OpenFileDialogを使う
以下のサイトを参考にしました。OSはWindows7 Ruby2.1です。
http://www3.ocn.ne.jp/~igus/starthp/ruby_memo.html
http://snjx.info/diary/snjx/052
VB風に作ってみました。
#! ruby -EWindows-31J # -*- mode:ruby; coding:Windows-31J -*- require 'win32ole' class RubyGUI def initialize @wsh = WIN32OLE.new('WScript.Shell') @sc=WIN32OLE.new("ScriptControl") @sc.language="VBScript" end def msgBox(message,msgBoxStyle="OKOnly",title="") style=@sc.eval(msgBoxStyle) @wsh.Popup(message,0, title,style) end def inputBox(message,title="") return @sc.eval('InputBox("' + "#{message}"+'"&vbcrlf&"","' + "#{title}"+ '")') end def type(s) return @sc.eval(s) end end class OpenFileDialog attr_accessor :maxFileSize,:filter,:initDir,:fileName def initialize @obj=WIN32OLE.new("MSComDlg.CommonDialog") @initDir="C:\\" @maxFileSize =255 @filter = "all (*.*)|*.*|" end def showDialog @obj.MaxFileSize =@maxFileSize @obj.Filter = @filter @obj.InitDir =@initDir @obj.ShowOpen @fileName=@obj.FileName end end msg=RubyGUI.new ofg=OpenFileDialog.new ret = msg.msgBox("終了しますか?","VbYesNo","終了確認") if ret == msg.type("VbYes") then msg.msgBox("終了しました") exit end msg.msgBox("ファイルを選択してください") filename = ofg.showDialog msg.msgBox("選択したファイルは" + filename + "です")
文字コード指定
こちらのサイトより
http://www.trail4you.com/TechNote/Ruby/basic.html
#! ruby -EWindows-31J
# -*- mode:ruby; coding:Windows-31J -*-
UTF-8の場合
2014-08-22
コマンドラインのエラーメッセージをファイルに出力する方法
rescueの部分でファイルに出力するといいそうです。
http://okwave.jp/qa/q8270092.html
begin VRLocalScreen.start(MyForm) ……※Formを実行している記述(一例です) rescue File.open("error.txt","w") do |f| f.puts $!.message f.puts $!.backtrace end end
$!は「最後に例外が発生したときの Exception オブジェクト」だそうです。
Ruby GUI
こちらのサイトを参考にしました。OSはWindows7 Ruby2.1です。
http://www3.ocn.ne.jp/~igus/starthp/ruby_memo.html
- msgbox
#! ruby require 'win32ole' wsh = WIN32OLE.new('WScript.Shell') wsh.Popup("hellow",0, "title")
- inputbox
require'win32ole' sc=WIN32OLE.new("ScriptControl") sc.language="VBScript" a=sc.eval('InputBox("hellow"&vbcrlf&"","title")') sc.eval(%Q! MsgBox("#{a}",0,"title") !)
- ファイル選択ダイアログ
#! ruby require 'win32ole' obj=WIN32OLE.new("MSComDlg.CommonDialog") obj.MaxFileSize =255 obj.Filter = "all (*.*)|*.*|" obj.InitDir ="C:\\" obj.ShowOpen file=obj.FileName wsh = WIN32OLE.new('WScript.Shell') if file=="" wsh.Popup("cansel",0, "error:") else wsh.Popup(file,0, "selected") end
2013-12-20
スクリプト実行中にputsの出力が表示されない場合の対策
RDEでprintやputs,getsの出力が表示されない問題と対策
Ruby入門: RDEでprintやputs,getsの出力が表示されない問題と対策 梶山 喜一郎
STDOUT.sync = true
を入れるといいそうです。
2013-10-04
Runnable
「プログラムコードのYouTube」を目指すRunnable。サンプルはサイト内で編集・実行可
「プログラムコードのYouTube」を目指すRunnable。サンプルはサイト内で編集・実行可 | TechCrunch Japan
2013-09-27
Ruby-2.0.0-p195 gemが動かない場合の対策方法
http://www.artonx.org/data/asr/
上記で公開されているRuby-2.0.0-p195をWindows7で動かしている時
gem updateやgem installを使おうとすると
Building native extensions. This could take a while...
と表示が出てそのまま動かなくなるのでその対策方法です。
1.Visual Studio 2010をインストールする
2.「Ruby-2.0 (Administrator) console」を実行してDOS窓を開く
3.「"C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"」を実行する
または以下。(上が32bitOSで下が64bitOSかな?)
「"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"」
この状態で「gem update」や gem install ○○ を実行するとちゃんとコンパイル出来るようになります。
追記
家の環境だとpathを設定しても上手くいかなかったので一度「Ruby-2.0 (Administrator) console」を起動して
path =""
path=C:/Program Files/It's Freetime Software/Ruby-2.0.0-x64/bin
を実行した後
「"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"」
を実行するとコンパイルするようになった。複数の開発環境(VS6.0~VS2012,各種マイコン用コンパイラ)が入っているのが原因かも知れない。
Updating installed gems
Updating bigdecimal
Building native extensions. This could take a while...
ERROR: Error installing bigdecimal:
ERROR: Failed to build gem native extension.
"C:/Program Files/It's Freetime Software/Ruby-2.0.0-x64/bin/ruby.exe" extcon
f.rb
C:/Program Files/It's Freetime Software/Ruby-2.0.0-x64/bin/ruby.exe: invalid swi
tch in RUBYOPT: -F (RuntimeError)
Gem files will remain installed in C:/Program Files/It's Freetime Software/Ruby-
2.0.0-x64/lib/ruby/gems/2.0.0/gems/bigdecimal-1.2.1 for inspection.
Results logged to C:/Program Files/It's Freetime Software/Ruby-2.0.0-x64/lib/rub
y/gems/2.0.0/gems/bigdecimal-1.2.1/./gem_make.out
それでもやっぱり一部エラーが出る。
http://d.hatena.ne.jp/kk_Ataka/comment/20111129/1322569582
こちらの記事を読むとパス名にスペースがあると起こる可能性があるらしい。
とりあえず一度Ruby2.0.0をアンインストールして「C:/Program Files/It's Freetime Software/Ruby-
とは別なパスにインストールしてみる。
インストールパスを「c:\Ruby-2.0.0-x64」に変えると予想通りエラー内容が変わったが、やはりbigdecimalでこける。
C:\Users\garyo>gem update
Updating installed gems
Updating bigdecimal
Building native extensions. This could take a while...
ERROR: Error installing bigdecimal:
ERROR: Failed to build gem native extension.
C:/RUBY-2~1.0-X/bin/ruby.exe extconf.rb
checking for labs() in stdlib.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--srcdir=.
--curdir
--ruby=C:/RUBY-2~1.0-X/bin/ruby
C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:431:in `try_do': The compiler failed to g
enerate an executable file. (RuntimeError)
You have to install development tools first.
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:516:in `try_link0'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:531:in `try_link'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:717:in `try_func'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:1001:in `block in have_func'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:892:in `block in checking_fo
r'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:337:in `block (2 levels) in
postpone'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:307:in `open'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:337:in `block in postpone'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:307:in `open'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:333:in `postpone'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:891:in `checking_for'
from C:/RUBY-2~1.0-X/lib/ruby/2.0.0/mkmf.rb:1000:in `have_func'
from extconf.rb:3:in `<main>'
Gem files will remain installed in C:/RUBY-2~1.0-X/lib/ruby/gems/2.0.0/gems/bigd
ecimal-1.2.1 for inspection.
Results logged to C:/RUBY-2~1.0-X/lib/ruby/gems/2.0.0/gems/bigdecimal-1.2.1/./ge
m_make.out
コンパイルできないと言っているので、もう一度パスを
を実行した後
「"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"」
を実行する。
C:\Users\garyo>"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvar
s32.bat"
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
ERROR: Cannot determine the location of the VS Common Tools folder.
と言ってくるので
path = %SystemRoot%\system32;%SystemRoot%;C:/Ruby-2.0.0-x64/bin
を実行して
「"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"」
を実行。今度は上手くいった。
しかし「gem update」を行うとやはりコンパイルエラーがでる。
VS2010に対応していないとかそういう話の気がしてきた。
C:\Users\garyo>gem update
Updating installed gems
Updating bigdecimal
Building native extensions. This could take a while...
ERROR: Error installing bigdecimal:
ERROR: Failed to build gem native extension.
C:/Ruby-2.0.0-x64/bin/ruby.exe extconf.rb
checking for labs() in stdlib.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers. Check the mkmf.log file for more details. You may
need configuration options.
Provided configuration options:
--with-opt-dir
--without-opt-dir
--with-opt-include
--without-opt-include=${opt-dir}/include
--with-opt-lib
--without-opt-lib=${opt-dir}/lib
--srcdir=.
--curdir
--ruby=C:/Ruby-2.0.0-x64/bin/ruby
C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:431:in `try_do': The compiler failed to
generate an executable file. (RuntimeError)
You have to install development tools first.
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:516:in `try_link0'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:531:in `try_link'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:717:in `try_func'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:1001:in `block in have_fun
c'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:892:in `block in checking_
for'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:337:in `block (2 levels) i
n postpone'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:307:in `open'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:337:in `block in postpone'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:307:in `open'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:333:in `postpone'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:891:in `checking_for'
from C:/Ruby-2.0.0-x64/lib/ruby/2.0.0/mkmf.rb:1000:in `have_func'
from extconf.rb:3:in `<main>'
Gem files will remain installed in C:/Ruby-2.0.0-x64/lib/ruby/gems/2.0.0/gems/bi
gdecimal-1.2.1 for inspection.
Results logged to C:/Ruby-2.0.0-x64/lib/ruby/gems/2.0.0/gems/bigdecimal-1.2.1/./
gem_make.out
CUI版のTwitter client tw
Tw: Twitter client on Ruby.
を実行すると
>tw
C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/dependency.rb:296:in `to_specs':
Could not find 'yajl-ruby' (~> 1.1.0) - did find: [yajl-ruby-1.1.0-x86-mswin32-6
0] (Gem::LoadError)
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/specification.rb:119
6:in `block in activate_dependencies'
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/specification.rb:118
5:in `each'
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/specification.rb:118
5:in `activate_dependencies'
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/specification.rb:116
7:in `activate'
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/specification.rb:119
9:in `block in activate_dependencies'
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/specification.rb:118
5:in `each'
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/specification.rb:118
5:in `activate_dependencies'
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/specification.rb:116
7:in `activate'
from C:/PROGRA~1/RUBY-2~1.0/lib/ruby/2.0.0/rubygems/core_ext/kernel_gem.
エラーが出て動かない。