Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby Cookbook

書籍 Ruby Cookbook | 配列のソート

パンくず Ruby Cookbook 配列のソート 概要 配列のソート サンプル require "pp" class Person attr_accessor:name,:age def initialize(name,age) @name,@age=name,age end end person_list = [Person.new("tanaka",23),Person.new("suzuki",30),Person.new…

書籍 Ruby Cookbook | 配列の重複要素削除

パンくず Ruby Cookbook 配列の重複要素削除 概要 配列の重複要素削除 サンプル array = [:one,:two,:three,:two] print array puts print array.uniq #=>不変メソッドで重複要素削除 puts print array puts print array.uniq! #=>破壊的メソッドで重複要素…

書籍 Ruby Cookbook | 配列一括設定

パンくず Ruby Cookbook 配列一括設定 概要 配列一括設定 サンプル array = [:one,:two,:three] a,b,c=array puts "#{a},#{b},#{c}" array << :four << :five a,b,*c=array puts "#{a},#{b},#{c}" 出力 one,two,three one,two,[:three, :four, :five]

書籍 Ruby Cookbook | Timeoutの制御

パンくず Ruby Cookbook Timeoutの制御 概要 Timeoutの制御 サンプル require 'timeout' def timeout_logic(limit_time, sleep_time) puts "limit_time=#{limit_time},sleep_time#{sleep_time}" Timeout::timeout(limit_time){ sleep sleep_time } rescue Ti…

書籍 Ruby Cookbook | 一定時間ごとに処理を実行

パンくず Ruby Cookbook 一定時間ごとに処理を実行 概要 一定時間ごとに処理を実行 サンプル # 指定したmsecごとに処理を実行する def execute_each_sec(sleep_sec) yield sleep sleep_sec end 5.times do execute_each_sec(1) do || puts "実行時間:#{Time.…

書籍 Ruby Cookbook | 日付同士の比較

パンくず Ruby Cookbook 日付同士の比較 概要 日付同士の比較 サンプル base_date = Date.new(2000,1,1) after_date = Date.new(2000,1,5) before_date = Date.new(1999,12,28) puts base_date puts (base_date - after_date).to_i puts (base_date - before…

書籍 Ruby Cookbook | 日付、日時の加算

パンくず Ruby Cookbook 日付、日時の加算 概要 日付、日時の加算 サンプル # encoding: Windows-31J require 'date' today = Date.today puts today puts today + 1 # => todayに1日足した結果 puts today - 1 # => todayから1日引いた結果 puts today >> 1…

書籍 Ruby Cookbook | 日付のイテレーション

パンくず Ruby Cookbook 日付のイテレーション 概要 日付のイテレーション サンプル # encoding: Windows-31J require 'date' today = Date.today ten_date_after = today + 10 now = Time.now puts today puts ten_date_after puts now puts "-------------…

書籍 Ruby Cookbook | 日付のフォーマット

パンくず Ruby Cookbook 日付のフォーマット 概要 日付のフォーマット フォーマット表 フォーマット用文字 内容 %A English day of the week %a Abbreviated English day of the week %B English month of the year %b English month of the year %C The cen…

書籍 Ruby Cookbook | 現在日時の取得

パンくず Ruby Cookbook 現在日時の取得 概要 現在日時の取得 サンプル now_date_time=DateTime.now now_time=Time.now now_date=Date.today puts now_date_time.to_s puts now_time.to_s puts now_date.to_s 出力 2012-06-19T00:41:31+09:00 2012-06-19 00:…

書籍 Ruby Cookbook | 数値リテラル

パンくず Ruby Cookbook 数値リテラル 概要 数値リテラル 構文 数値リテラル取得 10 0b10 0o10 0x10 サンプル puts 10 # => 10進数 puts 0b10 # => 2進数 puts 0o10 # => 8進数 puts 0x10 # => 16進数 出力 10 2 8 16

書籍 Ruby Cookbook | 乱数

パンくず Ruby Cookbook 乱数 概要 乱数 構文 乱数取得 num = rand 引数なし、デフォルトの場合は0から1の値が返ってくる。乱数取得(引数有り) num = rand(5) 0から指定した数値-1の数が返ってくる。 この場合は0から4。 サンプル (1..10).each {|num |put…

書籍 Ruby Cookbook | 分数

パンくず Ruby Cookbook 分数 概要 分数 サンプル require "rational" puts ra=Rational(2,3) puts ra.to_f puts ra*100 puts ra*Rational(3,2) puts ra+10 出力 2/3 0.6666666666666666 200/3 1/1 32/3

書籍 Ruby Cookbook | 文字列から基数変換

パンくず Ruby Cookbook 文字列から基数変換 概要 文字列から基数変換 サンプル puts "10".oct # => 8進数 puts "10".hex # => 16進数 # おまけ puts '10hoge'.to_i # => 先頭にある数値を抽出出来る puts 'hoge10hoge'.to_i # => 先頭以外の数値は抽出不可 …

書籍 Ruby Cookbook | 正規表現のUNION

パンくず Ruby Cookbook 正規表現のUNION 概要 正規表現のUNION 正規表現のUNION 以下の構文で正規表現のUNIONが可能です サンプル class String # => freezeはオブジェクトの凍結 def mgsub(key_value_pairs=[].freeze) # => Hashのキーを配列に変換 regexp…

書籍 Ruby Cookbook | メソッドの保持確認

パンくず Ruby Cookbook メソッドの保持確認 概要 メソッドの保持確認 メソッドの保持確認 以下の構文でメソッドの保持確認が可能です obj.respond_to?(:method) サンプル class Hoge def upcase(hoge);hoge.upcase;end end puts "hoge".respond_to?(:upcase…

書籍 Ruby Cookbook | 空白詰め、空白除去

パンくず Ruby Cookbook 空白詰め、空白除去 概要 空白詰め、空白除去 空白詰め 両端空白詰め(文字数指定) str.center(num) 右空白詰め(文字数指定) str.ljust(num) 左空白詰め(文字数指定) str.rjust(num) 空白除去 両端空白除去 str.strip 右空白除…

書籍 Ruby Cookbook | 大文字小文字変換

パンくず Ruby Cookbook 大文字小文字変換 概要 大文字小文字変換 大文字小文字変換 文字列全体を大文字にする場合 str.upcase 文字列全体を小文字にする場合 str.downcase 大文字小文字を変換する場合 str.swapcase 頭文字を大文字に、他を小文字にする場合…

書籍 Ruby Cookbook | 文字列を一文字ずつ抜き出して処理

パンくず Ruby Cookbook 文字列を一文字ずつ抜き出して処理 概要 文字列を一文字ずつ抜き出して処理 文字列を一文字ずつ抜き出して処理 正規表現を利用することで、文字列を一文字ずつ処理します。 サンプル 'hoge'.scan(/./) do |c| # =>正規表現で1文字ず…

書籍 Ruby Cookbook | eRuby

パンくず Ruby Cookbook eRuby 概要 eRuby eRuby eRubyによるテンプレート処理について。 eRubuy=JSPと同じ動きをすると思って差し支えない。 テンプレートとなるテキストにスクリプトレットでRubyのコードを埋め込んで 実行することができる。 サンプル req…

書籍 Ruby Cookbook | 変数を含む文字列とエスケープ

パンくず Ruby Cookbook 変数を含む文字列とエスケープ 概要 変数を含む文字列とエスケープ 変数を含む文字列とエスケープ 以下の構文で文字列内に変数を埋め込むことが出来ます。 hoge = "test" "#{hoge}!!" # => test!!と出力 この際、#{variable}の部分を…

書籍 Ruby Cookbook | Array#join

パンくず Ruby Cookbook Array#Join 概要 Array#join Array#Join Array#joinメソッドに文字列を指定することで、 指定文字列を間に挟んで、配列内の文字列連結を行います。 サンプル csv_columns = %w[one two three] puts csv_columns.join(",") # =>each文…

書籍 Ruby Cookbook | Predicate Method

パンくず Ruby Cookbook Predicate Method 概要 Predicate Method Predicate Method Predicate Method=述語メソッドRubyでは真偽値を返すメソッド=述語メソッドは末尾に?をつけます サンプル puts "".empty? # => true puts "hoge".empty? # => false # 自…

書籍 Ruby Cookbook | Immutable MethodとDestroy Method

パンくず Ruby Cookbook Immutable MethodとDestroy Method 概要 Immutable MethodとDestroy Method Immutable MethodとDestroy Method Immutable Method=不変なメソッド Destroy Method=破壊的なメソッド前者はオブジェクトの値を変えない。 後者はオブジ…

書籍 Ruby Cookbook | Stringの頭一文字を取得

パンくず Ruby Cookbook Stringの頭一文字を取得 概要 Stringの頭一文字を取得 サンプル hoge = "hoge" puts hoge.chr #=>hを返す

書籍 Ruby Cookbook

概要 書籍 Ruby Cookbookに関するメモ。 全てをカバーせず、自分の中で新たに学ぶことやまとめ直しておきたいことのみを抜粋します。 また、例示するサンプルも写経せずに出来るだけ自分で考えたサンプルにします。 各章 Chapter1. String Stringの頭一文字…