0
0

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 1 year has passed since last update.

投稿 133 ハッシュ 2024/11/10

0
Posted at

本日の勉強時間 3H

Rubyのハッシュを使った文字の置き換えは、キーと値の関係を利用して簡単に実現できます。ここでは、置き換えたい文字(キー)と、それを置き換える文字(値)をハッシュで管理する方法をわかりやすく説明します。

基本の考え方
Rubyのハッシュは、キー => 値のペアでデータを保存するデータ型です。キーを使って、そのキーに対応する値に素早くアクセスすることができます。

例えば、以下のように「文字」を置き換える場合を考えます:

「A」を「4」に
「E」を「3」に
「G」を「6」に
この置き換えを、ハッシュを使って表現してみましょう。

コードをコピーする
replace_map = {
  "A" => "4",
  "E" => "3",
  "G" => "6"
}

このreplace_mapハッシュでは、キー「A」には値「4」、キー「E」には値「3」が対応しています。

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?