8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

(Ryby)to_○メソッドの種類と処理をまとめてみました

Posted at

to_○メソッドの種類と処理内容をAからまとめてみました

範囲オブジェクト・ハッシュを配列に変換

to_a

(1..5).to_a => [1,2,3,4,5]
(a..e).to_a => [a,b,c,d,e]

currencies = {japan: "yen" , us: "dollar" , india: "rupee"}
currencies.to_a  
=>[[:japan,yen],[:us,dollar],[:india,rupee]]

文字数を複素数に変換

[複素数とは](https://www.buildinsider.net/language/rubytips/0007)
to_c 

"0.3-0.5i".to_c
=>(0.3-0.5i)

整数を少数に変換

``` to_f

10.to_f =>10.0
10.to_f / 4 => 2.5


<h2>配列をハッシュに変換</h2>

to_h

arry = [[:japan,yen],[:us,dollar],[:india,rupee]]
arry.to_h
=>{japan: "yen" , us: "dollar" , india: "rupee"}


<h2>文字列を数値に変換</h2>

to_i

10 + "3" => 103
10 + "3".to_i => 13


<h2>文字列を有理数に変換</h2>

to_r

"3/2".to_f
=> (3/2)


<h2>オブジェクトを文字列に変換</h2>

to_s

10.to_s => "10"
nil.to_s => ""
true.to_s => "true"


<h2>文字列をシンボルに変換</h2>

to_sym

string = "apple"
string.to_sym => :apple

参考文献
=>プロを目指す人のためのRuby入門
8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?