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 3 years have passed since last update.

to_symメソッド

Posted at

ハッシュの形

hash1 = { "anime" => "呪術廻戦"} ※キー文字列パターン

hash2 = { :anime=> "呪術廻戦"} ※キーシンボルパターン

hash3 = { anime: "呪術廻戦"} ※キーシンボルシンプルパターン

#hash1のパターン例
def movie_info(movie, data)
  puts movie[data]
end
movie = {"title" => "ハリーポッター", "genre" => "ファンタジー", "year" => "2001年"}
puts "以下から一つを選んで入力してください。
  ・title
  ・genre
  ・year"
info = gets.chomp   #CHECK!!
movie_info(movie, info)

入力を文字列でうけ、キーも文字列

#hash3のパターン例
def movie_info(movie, data)
  puts movie[data]
end
movie = {title: "ハリーポッター", genre: "ファンタジー",  year: "2001年"}
puts "以下から一つを選んで入力してください。
  ・title
  ・genre
  ・year"
info = gets.chomp.to_sym   def movie_info(movie, data)
  puts movie[data]
end
movie = {title: "ハリーポッター", genre: "ファンタジー",  year: "2001年"}
puts "以下から一つを選んで入力してください。
  ・title
  ・genre
  ・year"
info = gets.chomp.to_sym   #CHECK!!
movie_info(movie, info)  

入力を文字列でうけ、キーはシンボルのため、シンボルにへんこうしなければならない!

そんな時はコレ! ”to_symメソッド”

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?