å°å³ããã¦èª°ãæ°ãã¤ãã¦ããªãCRuby 2.5ã®æ°æ©è½
did_you_mean gemãKeyErrorã«ãå¹ãããã«ãªã£ãã
KeyError
ã¯æå®ããkeyã«å¯¾ããvalueãè¦ã¤ãããªãã£ãã¨ãã«èµ·ããä¾å¤ã§ãIndexError
ããæ´¾çãããã®ã§ãã
KeyError
ãèµ·ããå¾ãã®ã¯Hash#fetch
Hash#fetch_values
ENV.fetch
Kernel.sprintf
String#%
ã®5ã¤ã§ãã
ãã®ã¡ã½ããéã§ãæ¢ããkeyãè¦ã¤ãããªãã£ãã¨ãã«è¿ããåè£ããµã¸ã§ã¹ããã¦ãããããã«ãªã£ã¦ãã¾ãã
$ ruby h = {foo: 1, bar: 2} h.fetch(:bax) Traceback (most recent call last): 1: from -:2:in `<main>' -:2:in `fetch': key not found: :bax (KeyError) Did you mean? :bar
å®ã¯CRuby 2.4ã§ãrequire "did_you_mean/experimental/key_error_name_correction"
ã¨åªæãå±ããã¨ãHash#fetch
ã«å¯¾ãã¦did_you_meanãå¹ãã¦ããã®ã§ãããããæ¹ã«æ°ã¥ããªãä¸ã«Hash#fetch
ãprepend
ã§ã¢ã³ãã¼ãããããå±ãªãå®è£
ã ã£ãã®ã§èª°ã使ã£ã¦ãã¾ããã§ããã*1
ããã¯ã¤ã«ã³ã¨ä¸å¿µçºèµ·ãã KeyError
ã«2ã¤ã¡ã½ããã追å ãã¾ããã
ããã§ãNameError
ã¨åãããã¨ã©ã¼ãªãã¸ã§ã¯ãã«åå ã¨ãªããªãã¸ã§ã¯ããç´ä»ãããã¨ãã§ããã®ã§ãã¢ã³ãã¼ãããç¡ãã§did_you_meanãå¹ããããã¨ãã§ãã¾ãã
ãã®è¾ºã§æãã¤ããæ©è½ãã¤ãã«å®ãçµãã ããã§ããã
RubyでJSONをclassにmappingするやつと、2つの記法 - スペクトラム
ãµãã£ã¦ãå©ç¨ä¸ããã*2
CRubyã«åãå ¥ãããå°å³ããã¦ã©ãã«ãç´¹ä»ããã¦ããªãæ°æ©è½ã試ãã¦ãã¤ãã¤ãã¦ã
— ã¬ããã³ (@_ksss_) 2017å¹´12æ26æ¥
*1:ä½è ã¯ç§ã§ã
*2:æ¬å½ã¯did_you_meanä½è ã®yuki24ãããRubyã®ã¢ããã³ãã«ã¬ã³ãã¼ã«ç»é²ãã¦ããã®ãè¦ã¦ãããã§ç´¹ä»ãããã ããã¨æã£ã¦ããã©ãã¢ããã³ãã«ã¬ã³ãã¼ã®ç»é²ãæ¶ããã£ã½ãã®ã§ãã®ã¿ã¤ãã³ã°ã«ã