LoginSignup
2
0

More than 5 years have passed since last update.

Ruby 知っとけ技

Last updated at Posted at 2017-11-24

いつもいつも書き方を忘れるので、メモ書き

Hashから値だけを取り出す

Hash#valuesを使う

sample.rb
profile = {"name" => "TomoProg", "blood" => "AB"}
profile.values # ["TomoProg", "AB"]

配列の要素をとある文字で区切りたい

Array#joinを使う

sample.rb
profile = ["TomoProg", "AB"]
profile.join      # "TomoProgAB"
profile.join(',') # "TomoProg,AB"

文字列をとある文字で区切りたい

String#splitを使う

sample.rb
profile = "TomoProg,AB"
profile.split(',') # ["TomoProg", "AB"]

数値をカンマ区切りにしたい

to_s(:delimited)を使う

sample.rb
num = 123456789
num.to_s(:delimited) # "123,456,789"
2
0
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
2
0