Created
November 18, 2013 01:43
-
-
Save kidach1/7521070 to your computer and use it in GitHub Desktop.
Railsを触る際知っていると便利なRubyの基礎 [ブロックとかシンボルとか] ref: http://qiita.com/kidachi_/items/46a6e49b6306655ccd64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#配列要素の分割 | |
> "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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#配列要素の追加 | |
> 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> hash.each do |key, value| | |
>> puts "Key #{key} has value #{value}" | |
>> end | |
Key success has value It worked! | |
Key error has value It failed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= stylesheet_link_tag "application", media: "all", | |
"data-turbolinks-track" => true %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stylesheet_link_tag("application", media: "all", | |
"data-turbolinks-track" => true) | |
stylesheet_link_tag "application", media: "all", | |
"data-turbolinks-track" => true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stylesheet_link_tag "application", { media: "all", | |
"data-turbolinks-track" => true } | |
stylesheet_link_tag "application", media: "all", | |
"data-turbolinks-track" => true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data-turbolinks-track: true #NG。シンボルではハイフンを使用できない | |
"data-turbolinks-track" => true #OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link data-turbolinks-track="true" href="/assets/application.css" media="all" rel="stylesheet" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! " |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> a = "hoge" | |
=> "hoge" | |
#明示的な書き方 | |
>> a = String.new("hoge") | |
=> "hoge" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> a = [1, 3, 2] | |
=> [1, 3, 2] | |
#明示的な書き方 | |
>> a = Array.new([1, 3, 2]) | |
=> [1, 3, 2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 何も値を渡さずに初期化 | |
>> h = Hash.new | |
=> {} | |
>> h[:foo] # 存在しないキー :foo | |
=> nil # キーが存在しない場合のデフォルト値はnil | |
# 値を渡して初期化 | |
>> h = Hash.new(0) # デフォルト値をnilから0に変更 | |
=> {} | |
>> h[:foo] | |
=> 0 # キーが存在しない場合のデフォルト値は0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#「.」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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> 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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> hash.each do |key, value| | |
?> puts "Key #{key} has value #{value}" | |
>> end | |
Key success has value It worked! | |
Key error has value It failed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= stylesheet_link_tag "application", media: "all", | |
"data-turbolinks-track" => true %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stylesheet_link_tag("application", media: "all", | |
"data-turbolinks-track" => true) | |
stylesheet_link_tag "application", media: "all", | |
"data-turbolinks-track" => true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stylesheet_link_tag "application", { media: "all", | |
"data-turbolinks-track" => true } | |
stylesheet_link_tag "application", media: "all", | |
"data-turbolinks-track" => true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data-turbolinks-track: true #NG。シンボルではハイフンを使用できない | |
"data-turbolinks-track" => true #OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<link data-turbolinks-track="true" href="/assets/application.css" media="all" rel="stylesheet" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> a = "hoge" | |
=> "hoge" | |
#明示的な書き方 | |
>> a = String.new("hoge") | |
=> "hoge" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> a = [1, 3, 2] | |
=> [1, 3, 2] | |
#明示的な書き方 | |
>> a = Array.new([1, 3, 2]) | |
=> [1, 3, 2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 何も値を渡さずに初期化 | |
>> h = Hash.new | |
=> {} | |
>> h[:foo] # 存在しないキー :foo | |
=> nil # キーが存在しない場合のデフォルト値はnil | |
# 値を渡して初期化 | |
>> h = Hash.new(0) # デフォルト値をnilから0に変更 | |
=> {} | |
>> h[:foo] | |
=> 0 # キーが存在しない場合のデフォルト値は0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#文字列を配列に変換 | |
> b = %w[foo bar baz fuga moga] | |
=> ["foo", "bar", "baz", "fuga", "moga"] | |
#配列から指定範囲を取り出す | |
> b[0..2] | |
=> ["foo", "bar", "baz"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#配列へ変換 | |
> 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#範囲は文字列に対しても利用出来る | |
> ('a'..'e').to_a | |
=> ["a", "b", "c", "d", "e"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#文字列は分割できる | |
>> "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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ :hoge => 'hoge', :fuga => 'fuga'} | |
{ hoge: 'hoge', fuga: 'fuga'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> 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