Skip to content

Instantly share code, notes, and snippets.

@kidach1
Created November 18, 2013 01:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidach1/7521070 to your computer and use it in GitHub Desktop.
Save kidach1/7521070 to your computer and use it in GitHub Desktop.
Railsを触る際知っていると便利なRubyの基礎 [ブロックとかシンボルとか] ref: http://qiita.com/kidachi_/items/46a6e49b6306655ccd64
#配列要素の分割
> "foo bar bazz".split
=> ["foo", "bar", "bazz"]
#配列要素の指定文字列での分割
> "fooxbarxxxxbazz".split('x')
=> ["foo", "bar", "", "", "", "bazz"]
#配列要素の順番入れ替え
> "foo bar bazz".split.reverse
=> ["bazz", "bar", "foo"]
#配列要素の順番入れ替え
> a = "foo bar bazz".split.shuffle
=> ["bar", "foo", "bazz"]
#配列要素の追加
> a.push(8)
=> ["bar", "foo", "bazz", 8]
#配列要素の追加2
> a << 10
=> ["bar", "foo", "bazz", 8, 10]
#配列要素の追加3(chain)
> a << 'fuga' << 'moga'
=> ["bar", "foo", "bazz", 8, 10, "fuga", "moga"]
#配列要素の結合
> a.join
=> "barfoobazz810fugamoga"
#配列要素の結合2
> a.join(', ')
=> "bar, foo, bazz, 8, 10, fuga, moga"
>> hash.each do |key, value|
>> puts "Key #{key} has value #{value}"
>> end
Key success has value It worked!
Key error has value It failed.
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
stylesheet_link_tag("application", media: "all",
"data-turbolinks-track" => true)
stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true
stylesheet_link_tag "application", { media: "all",
"data-turbolinks-track" => true }
stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true
data-turbolinks-track: true #NG。シンボルではハイフンを使用できない
"data-turbolinks-track" => true #OK
<link data-turbolinks-track="true" href="/assets/application.css" media="all" rel="stylesheet" />
string_proc = Proc.new do |x, string|
p string * x
end
magic_five_box('happy_proc! ', string_proc)
=> "happy_proc! happy_proc! happy_proc! happy_proc! happy_proc! "
>> a = "hoge"
=> "hoge"
#明示的な書き方
>> a = String.new("hoge")
=> "hoge"
>> a = [1, 3, 2]
=> [1, 3, 2]
#明示的な書き方
>> a = Array.new([1, 3, 2])
=> [1, 3, 2]
# 何も値を渡さずに初期化
>> h = Hash.new
=> {}
>> h[:foo] # 存在しないキー :foo
=> nil # キーが存在しない場合のデフォルト値はnil
# 値を渡して初期化
>> h = Hash.new(0) # デフォルト値をnilから0に変更
=> {}
>> h[:foo]
=> 0 # キーが存在しない場合のデフォルト値は0
module ApplicationHelper
#「.」2つで後方の要素(9)を含む
> (0..9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#「.」3つで後方の要素(9)は含まない
> (0...9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8]
>> hash = { success: "It worked!", error: "It failed." }
=> {:success=>"It worked!", :error=>"It failed."}
>> hash.each do |key, value|
?> puts "Key #{key.inspect} has value #{value.inspect}"
>> end
Key :success has value "It worked!"
Key :error has value "It failed."
>> hash.each do |key, value|
?> puts "Key #{key} has value #{value}"
>> end
Key success has value It worked!
Key error has value It failed.
<%= stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true %>
stylesheet_link_tag("application", media: "all",
"data-turbolinks-track" => true)
stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true
stylesheet_link_tag "application", { media: "all",
"data-turbolinks-track" => true }
stylesheet_link_tag "application", media: "all",
"data-turbolinks-track" => true
data-turbolinks-track: true #NG。シンボルではハイフンを使用できない
"data-turbolinks-track" => true #OK
<link data-turbolinks-track="true" href="/assets/application.css" media="all" rel="stylesheet" />
>> a = "hoge"
=> "hoge"
#明示的な書き方
>> a = String.new("hoge")
=> "hoge"
>> a = [1, 3, 2]
=> [1, 3, 2]
#明示的な書き方
>> a = Array.new([1, 3, 2])
=> [1, 3, 2]
# 何も値を渡さずに初期化
>> h = Hash.new
=> {}
>> h[:foo] # 存在しないキー :foo
=> nil # キーが存在しない場合のデフォルト値はnil
# 値を渡して初期化
>> h = Hash.new(0) # デフォルト値をnilから0に変更
=> {}
>> h[:foo]
=> 0 # キーが存在しない場合のデフォルト値は0
#文字列を配列に変換
> b = %w[foo bar baz fuga moga]
=> ["foo", "bar", "baz", "fuga", "moga"]
#配列から指定範囲を取り出す
> b[0..2]
=> ["foo", "bar", "baz"]
module ApplicationHelper
#配列へ変換
> c = (0..9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
#配列の最後の要素指定
> c[5..(c.length-1)]
=> [5, 6, 7, 8, 9]
#配列の最後の要素指定2
> c[5..-1]
=> [5, 6, 7, 8, 9]
#範囲は文字列に対しても利用出来る
> ('a'..'e').to_a
=> ["a", "b", "c", "d", "e"]
#文字列は分割できる
>> "name".split('')
=> ["n", "a", "m", "e"]
#シンボルは分割できない
>> :name.split('')
NoMethodError: undefined method `split' for :name:Symbol
#文字列は(分割して)reverseできる
>> "foobar".reverse
=> "raboof"
#シンボルは(分割して)reverseできない
>> :foobar.reverse
NoMethodError: undefined method `reverse' for :foobar:Symbol
{ :hoge => 'hoge', :fuga => 'fuga'}
{ hoge: 'hoge', fuga: 'fuga'}
>> h1 = { :name => "hogefuga", :email => "foo@example.com" }
=> {:name=>"hogefuga", :email=>"foo@example.com"}
>> h2 = { name: "hogefuga", email: "foo@example.com" }
=> {:name=>"hogefuga", :email=>"foo@example.com"}
>> h1 == h2
=> true
>> hash = { success: "It worked!", error: "It failed." }
=> {:success=>"It worked!", :error=>"It failed."}
>> hash.each do |key, value|
>> puts "Key #{key.inspect} has value #{value.inspect}"
>> end
Key :success has value "It worked!"
Key :error has value "It failed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment