leaning diary Rails

【Learning Diary49】Hash#clear/空のハッシュオブジェクト作成/unlessはelseのみ/inject/chop/index/append/rewind

05/12/2023

【Learning Diary49】Hash#clear/空のハッシュオブジェクト作成/unlessはelseのみ/inject/chop/index/append/rewind

 

Hash#clear

ハッシュの中身を空にするメソッドです。

 

「!」はついていませんが、破壊的メソッドです。

 

なお、デフォルト値はクリアされません。

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
❯ irb
>> h = {a: 100, b: 200}
=> {:a=>100, :b=>200}
>> h.clear
>> p h
{}
=> {}
>> h = Hash.new("default value")
=> {}
>> h.clear
=> {}
>> h.default
=> "default value"
>>
❯ irb >> h = {a: 100, b: 200} => {:a=>100, :b=>200} >> h.clear >> p h {} => {} >> h = Hash.new("default value") => {} >> h.clear => {} >> h.default => "default value" >>
❯ irb
>> h = {a: 100, b: 200}
=> {:a=>100, :b=>200}
>> h.clear
>> p h
{}
=> {}
>> h = Hash.new("default value")
=> {}
>> h.clear
=> {}
>> h.default
=> "default value"
>>

 

参照:instance method Hash#clear

 

空のハッシュオブジェクトを作成する方法

 

■Hash({})

■{}

■Hash.new

 

unlessはelseのみ(elsif不可)

elsifは併用できないためエラーが起こります。

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
?> def hoge(n)
?> unless n != 3
?> "hello"
?> elsif n == 5
?> "world"
?> end
>> end
/Users/tokudome/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.9.1/lib/irb/workspace.rb:117:in `eval': (irb):11: syntax error, unexpected `elsif', expecting `end' or dummy end (SyntaxError)
elsif n == 5
^~~~~
# elseであれば用いることができる
?> def hoge(n)
?> unless n != 3
?> "hello"
?> else n == 5
?> "world"
?> end
>> end
=> :hoge
>> str = ''
=> ""
>> str.concat hoge(3)
=> "hello"
>> str.concat hoge(5)
=> "helloworld"
>>
>> puts str
helloworld
?> def hoge(n) ?> unless n != 3 ?> "hello" ?> elsif n == 5 ?> "world" ?> end >> end /Users/tokudome/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.9.1/lib/irb/workspace.rb:117:in `eval': (irb):11: syntax error, unexpected `elsif', expecting `end' or dummy end (SyntaxError) elsif n == 5 ^~~~~ # elseであれば用いることができる ?> def hoge(n) ?> unless n != 3 ?> "hello" ?> else n == 5 ?> "world" ?> end >> end => :hoge >> str = '' => "" >> str.concat hoge(3) => "hello" >> str.concat hoge(5) => "helloworld" >> >> puts str helloworld
?> def hoge(n)
?>   unless n != 3
?>     "hello"
?>     elsif n == 5
?>     "world"
?>   end
>> end
/Users/tokudome/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.9.1/lib/irb/workspace.rb:117:in `eval': (irb):11: syntax error, unexpected `elsif', expecting `end' or dummy end (SyntaxError)
    elsif n == 5
    ^~~~~


# elseであれば用いることができる

?> def hoge(n)
?>   unless n != 3
?>     "hello"
?>   else n == 5
?>     "world"
?>   end
>> end
=> :hoge
>> str = ''
=> ""
>> str.concat hoge(3)
=> "hello"
>> str.concat hoge(5)
=> "helloworld"
>>
>> puts str
helloworld

 

instance method Enumerable#inject

Enumerable#injectは、たたみこみ演算を行うメソッドです。

 

■引数を省略した場合は、要素1がブロック引数の1番目に渡されます。
■引数を指定した場合は、その値が初期値になります。
■ブロック引数の1番目は前回の戻り値が渡されます。初回は、初期値が渡されます。
■ブロック引数の2番目は要素が順番に渡されます。

 

参照:instance method Enumerable#inject

 

String.chop

String.chopは文字列の最後の文字を取り除いた、新しい文字列を生成して返します。

 

文字列の終端が "\r\n" であればその 2 文字を取り除きます。

 

参照:instance method String#chop

 

instance method String#index

 

String#indexは、index(pattern, pos = 0)の形で使います。

 

文字列のpos番目からpatternを検索し、最初に見つかった位置を返します。

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>> a = "Ruby"
=> "Ruby"
>> b = " on Rails"
=> " on Rails"
>> a.concat b
=> "Ruby on Rails"
>> a.reverse!
=> "sliaR no ybuR"
>> p a.index("R", 1)
4
=> 4
>> a = "Ruby" => "Ruby" >> b = " on Rails" => " on Rails" >> a.concat b => "Ruby on Rails" >> a.reverse! => "sliaR no ybuR" >> p a.index("R", 1) 4 => 4
>> a = "Ruby"
=> "Ruby"
>> b = " on Rails"
=> " on Rails"
>> a.concat b
=> "Ruby on Rails"
>> a.reverse!
=> "sliaR no ybuR"
>> p a.index("R", 1)
4
=> 4

 

参照:instance method String#index

 

Array#append

append はStringには使用できないメソッドです。

 

レシーバにはArrayを取ります。

 

appendはpushと同様、引数を配列の末尾に追加します。

 

引数を指定しない場合は何も起こりません。

 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
>> a = "Ruby"
=> "Ruby"
>> b = " on Rails"
=> " on Rails"
>> a.append b
(irb):47:in `<main>': undefined method `append' for "Ruby":String (NoMethodError)
from /Users/tokudome/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.9.1/exe/irb:9:in `<top (required)>'
from /Users/tokudome/.rbenv/versions/3.2.2/bin/irb:25:in `load'
from /Users/tokudome/.rbenv/versions/3.2.2/bin/irb:25:in `<main>'
>> a.reverse
=> "ybuR"
>> p a
"Ruby"
=> "Ruby"
>> a.index("R", 1)
=> nil
>>
>> a = "Ruby" => "Ruby" >> b = " on Rails" => " on Rails" >> a.append b (irb):47:in `<main>': undefined method `append' for "Ruby":String (NoMethodError) from /Users/tokudome/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.9.1/exe/irb:9:in `<top (required)>' from /Users/tokudome/.rbenv/versions/3.2.2/bin/irb:25:in `load' from /Users/tokudome/.rbenv/versions/3.2.2/bin/irb:25:in `<main>' >> a.reverse => "ybuR" >> p a "Ruby" => "Ruby" >> a.index("R", 1) => nil >>
>> a = "Ruby"
=> "Ruby"
>> b = " on Rails"
=> " on Rails"
>> a.append b
(irb):47:in `<main>': undefined method `append' for "Ruby":String (NoMethodError)
    from /Users/tokudome/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/irb-1.9.1/exe/irb:9:in `<top (required)>'
    from /Users/tokudome/.rbenv/versions/3.2.2/bin/irb:25:in `load'
    from /Users/tokudome/.rbenv/versions/3.2.2/bin/irb:25:in `<main>'
>> a.reverse
=> "ybuR"
>> p a
"Ruby"
=> "Ruby"
>> a.index("R", 1)
=> nil
>>

 

参照:instance method Array#append

 

instance method IO#rewind

ファイルポインタを先頭に持ってくるメソッドです。

 

参照:instance method IO#rewind

-leaning diary, Rails