LoginSignup
43
27

More than 5 years have passed since last update.

no implicit conversion of String into Integer の対処について【Ruby】

Posted at

概要

PAYJPを使って支払の処理をするのに、JSONに慣れておらず、またrubyの理解不足でなかなかデータを取り出せなかったのでその備忘録です。
初心者ゆえ何か間違い等ありましたらご指摘くださいませ。。

具体例

コントローラーでbinding.pryをして、
customer = Payjp::Customer.createした
カスタマーのカード情報を取り出します。

pry(#)> customer[:cards]
     #Payjp::ListObject:0x3feb68d9adbc JSON: {
"count": 1,
"data": [
{"id":"ca......
],
"has_more": false,
"object": "list",
"url": "/v1/customers/cus_96a3f6019fb2023a6d784...../cards"

customer[:cards][:data]とすると。。
=> [# JSON: {
"id": "car_276ef**********",
"address_city": null,
"address_line1": null,
"address_line2": null,
"address_state": null,
"address_zip": null,
"address_zip_check": "unchecked",
"brand": "Visa",
"country": null,
"created": 1550055737,
"customer": "cus_96a3f6019fb2023a6
***",
"cvc_check": "passed",
"exp_month": 10,
"exp_year": 2020,
"fingerprint": "e1d8225886e3a7211127d
******",
"last4": "4242",
"livemode": false,
"metadata": {},
"name": "AAA AAA",
"object": "card"
}]

ここからカードブランドがvisaてところを取り出したい。
とりあえずcustomer[:cards][:data][:brand]としてみるものの。。。

TypeError: no implicit conversion of Symbol into Integerとなる際の解決法

TypeError: no implicit conversion of Symbol into Integerとなる。

ここでkeyがシンボルではなく文字列であることに気づいた!!

初歩的ミス。。。

customer[:cards][:data]["brand"]とすると。。。

TypeError: no implicit conversion of String into Intege

まだダメ。。。

しかし!よく見たらdataの後ろに"["があり、これは配列を意味するので、
このようにして無事取り出せました。

customer[:cards][:data][0]["brand"]
=> "Visa"

 参考にした記事

https://qiita.com/QUANON/items/169c73425a6bc50dee51
https://stackoverflow.com/questions/20790499/no-implicit-conversion-of-string-into-integer-typeerror

43
27
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
43
27