内容へ移動
TechNote
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
現在位置:
start
»
ruby
»
basic
ruby:basic
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== Ruby - 基礎 ====== ここでの解説はRuby 2.00を前提にしています。 ===== インストール(MacOS 10.7,10.8,10.9) ===== <sxh bash; gutter: false> $ ./configure --prefix=/usr/local --with-opt-dir=/usr/local --enable-shared --enable-pthread $ make $ sudo make install </sxh> Mac OSの一部ではrubyのインストールは正常終了してもgemを使う時に以下のようなエラーが発生する。 <sxh bash; gutter: false> $ gem install json ERROR: Loading command: install (LoadError) cannot load such file -- openssl ERROR: While executing gem ... (NoMethodError) undefined method `invoke_with_build_args' for nil:NilClass </sxh> これはOpenSSLのバージョンが古いことに起因しているらしいので、まず最新のOpenSSLをインストールする。 * [[https://www.openssl.org/source/openssl-1.0.2d.tar.gz|openssl-1.0.2d.tar.gz]] <sxh bash; gutter: false> $ ./configure darwin64-x86_64-cc --prefix=/usr/local --openssldir=/usr/local/openssl $ make $ sudo make install </sxh> その後、--with-opt-dir=/usr/localオプションを使用してRubyを再度インストール。 <sxh bash; gutter: false> $ ./configure --prefix=/usr/local --with-opt-dir=/usr/local --enable-shared --enable-pthread $ make $ sudo make install </sxh> ===== インストール(MacOS 10.5,10.6) ===== <sxh bash; gutter: false> $ ./configure --prefix=/usr/local --enable-shared --enable-pthread $ make $ sudo make install </sxh> gem実行時に以下のエラーが出た場合は、LibYAMLをインストールして環境変数を設定した後、Rubyを再度インストールすることで解決する。 It seems your ruby installation is missing psych (for YAML output). To eliminate this warning, please install libyaml and reinstall your ruby. * **[[http://pyyaml.org/wiki/LibYAML|LibYAML]]** <sxh bash; gutter: false> $ ./configure $ make $ sudo make install </sxh> 環境変数に以下を加える。 export DYLD_LIBRARY_PATH=/usr/local/lib:$DYLD_LIBRARY_PATH ===== インストール(Windows 7 64bit) ===== 以下よりRuby 2.0.0-p0 (x64)とDevKit-mingw64-64-4.7.2-20130224-1432-sfx.exeをダウンロード。 * **[[http://rubyinstaller.org/downloads/|RubyInstaller]]** それぞれRubyはC:/usr、DevKitはC:/DevKitにインストール。 Windowsの環境変数「PATH」に以下を追加。 C:\usr\bin;C:\usr\lib;C:\usr\include;C:\DevKit\bin;C:\DevKit\lib;C:\DevKit\include;C:\DevKit\mingw\bin;C:\DevKit\mingw\lib;C:\DevKit\mingw\include DevKitの設定 <sxh bash; gutter: false> $ cd C:/DevKit $ ruby dk.rb init </sxh> C:/DevKit/config.ymlができるので以下のように編集。 <sxh bash; gutter: false> # This configuration file contains the absolute path locations of all # installed Rubies to be enhanced to work with the DevKit. This config # file is generated by the 'ruby dk.rb init' step and may be modified # before running the 'ruby dk.rb install' step. To include any installed # Rubies that were not automagically discovered, simply add a line below # the triple hyphens with the absolute path to the Ruby root directory. # # Example: # # --- # - C:/ruby19trunk # - C:/ruby192dev # --- - C:/usr </sxh> 以下コマンドを実行して反映。 <sxh bash; gutter: false> $ ruby dk.rb install </sxh> Gemでriとrdocをインストールしないように設定。\\ C:/Users/<username>/.gemrc を作成して以下を追加。 gem: --no-ri --no-rdoc ===== ソースファイルの文字コードを指定する ===== <sxh ruby> #!/usr/bin/ruby # coding: utf-8 </sxh> ===== ファイル/ディレクトリ操作 ===== ==== ファイル入出力時の文字コードを指定する ==== <sxh ruby> #!/usr/bin/ruby # coding: utf-8 # 入力 file = File.open("Shift_JIS.txt","r:sjis") # 処理 file.close # 出力 file = File.open("Shift_JIS.txt","w:sjis") # 処理 file.close </sxh> ==== 多階層ディレクトリの作成方法 ==== <sxh ruby> #!/usr/bin/ruby # coding: utf-8 path = "/AAA/BBB/CCC/DDD/EEE" FileUtils.mkpath(path) </sxh> === オプション一覧 === ^オプション^効果^ |:mode |パーミッションを8進数で指定(Example: 0777)| |:noop |trueを指定すると実際の処理は行わない| |:verbose|trueを指定すると標準出力に詳細を出力| === オプション使用例 === <sxh ruby> # ディレクトリ作成時のパーミッションを指定通りにするため予めumaskに0を設定 File.umask(0) FileUtils.mkpath(path,{:mode => 0777, :noop => true, :verbose => true}) </sxh> ===== トランスコーディング ===== <sxh ruby> #!/usr/bin/ruby # coding: utf-8 # UTF-8文字列 strings_utf8 = "あいうえお" puts strings_utf8.encoding # => #<Encoding:UTF-8> # SJIS変換 strings_sjis = strings_utf8.encode("sjis") puts strings_sjis.encoding # => #<Encoding:Windows-31J> </sxh> <color red> 但し、変換後の文字コードでは使用できない文字が含まれていると、エラーになる。\\ __以下のように不正文字を置換文字列で置換えることで対処できる。__ </color> <sxh ruby> #!/usr/bin/ruby # coding: utf-8 # UTF-8文字列 strings_utf8 = "SJISでは使用できない文字 => ©" puts strings_utf8.encoding # => #<Encoding:UTF-8> # SJIS変換 strings_sjis = strings_utf8.encode("sjis", undef: :replace, replace: "<??>") puts strings_sjis.encoding # => #<Encoding:Windows-31J> </sxh> ===== オブジェクト指向 ===== ==== アクセサメソッド ==== <sxh ruby> # ゲッターメソッドを生成 attr_reader :name # セッターメソッドを生成 attr_writer :name # ゲッターメソッド/セッターメソッドを生成 attr_accessor :name </sxh> <sxh ruby> # ------------------------------------------------------ class Test # ------------------------------------------------------ attr_reader :name1 attr_writer :name2 attr_accessor :name3 # -------------------------------------------------- def initialize(name1,name2,name3) # -------------------------------------------------- @name1 = name1 @name2 = name2 @name3 = name3 end end test = Test.new("MyName1","MyName2","MyName3") test.name1 = "NewName1" # ERROR: Read Only puts test.name1 # -> MyName1 test.name2 = "NewName2" # OK puts test.name2 # ERROR: Write Only test.name3 = "NewName3" # OK puts test.name3 # -> NewName3 </sxh>
ruby/basic.txt
· 最終更新: 2018/07/20 14:18 by
taka
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ