ユーザ用ツール

サイト用ツール


ruby:basic

Ruby - 基礎

ここでの解説はRuby 2.00を前提にしています。

インストール(MacOS 10.7,10.8,10.9)

$ ./configure --prefix=/usr/local --with-opt-dir=/usr/local --enable-shared --enable-pthread
$ make
$ sudo make install

Mac OSの一部ではrubyのインストールは正常終了してもgemを使う時に以下のようなエラーが発生する。

$ 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

これはOpenSSLのバージョンが古いことに起因しているらしいので、まず最新のOpenSSLをインストールする。

$ ./configure darwin64-x86_64-cc --prefix=/usr/local --openssldir=/usr/local/openssl
$ make
$ sudo make install

その後、–with-opt-dir=/usr/localオプションを使用してRubyを再度インストール。

  $ ./configure --prefix=/usr/local --with-opt-dir=/usr/local --enable-shared --enable-pthread
  $ make
  $ sudo make install

インストール(MacOS 10.5,10.6)

$ ./configure --prefix=/usr/local --enable-shared --enable-pthread
$ make
$ sudo make install

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.

$ ./configure
$ make
$ sudo make install

環境変数に以下を加える。

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をダウンロード。

それぞれ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の設定

$ cd C:/DevKit
$ ruby dk.rb init

C:/DevKit/config.ymlができるので以下のように編集。

# 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

以下コマンドを実行して反映。

$ ruby dk.rb install

Gemでriとrdocをインストールしないように設定。
C:/Users/<username>/.gemrc を作成して以下を追加。

gem: --no-ri --no-rdoc

ソースファイルの文字コードを指定する

#!/usr/bin/ruby
# coding: utf-8

ファイル/ディレクトリ操作

ファイル入出力時の文字コードを指定する

#!/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

多階層ディレクトリの作成方法

#!/usr/bin/ruby
# coding: utf-8

path = "/AAA/BBB/CCC/DDD/EEE"

FileUtils.mkpath(path)

オプション一覧

オプション効果
:mode パーミッションを8進数で指定(Example: 0777)
:noop trueを指定すると実際の処理は行わない
:verbosetrueを指定すると標準出力に詳細を出力

オプション使用例

# ディレクトリ作成時のパーミッションを指定通りにするため予めumaskに0を設定
File.umask(0)
FileUtils.mkpath(path,{:mode => 0777, :noop => true, :verbose => true})

トランスコーディング

#!/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>

但し、変換後の文字コードでは使用できない文字が含まれていると、エラーになる。
以下のように不正文字を置換文字列で置換えることで対処できる。

#!/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>

オブジェクト指向

アクセサメソッド

# ゲッターメソッドを生成
attr_reader   :name
# セッターメソッドを生成
attr_writer   :name
# ゲッターメソッド/セッターメソッドを生成
attr_accessor :name

# ------------------------------------------------------
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

ruby/basic.txt · 最終更新: 2018/07/20 14:18 by taka