REx - Ruby Examinationで出てきた問題を振り返る。
以下のコードを実行するとどうなるか?
hoge = "a".to_h puts hoge.class
解答
nilエラーがでる。なぜならa
はStringで、Stringクラスにto_h
メソッドは使えない。
irb(main):001:0> hoge = "a".to_h NoMethodError (undefined method `to_h' for "a":String) Did you mean? to_f to_i to_s to_c to_r irb(main):002:0> puts hoge.class NilClass => nil