Error comparing variables: initialized constant BUY (NameError)

Asked 2 years ago, Updated 2 years ago, 31 views

When json takes a value, substitutes a variable, and compares it with an uninitialized constant, an error occurs."I would like to display ""yes"" when a==BUY."
Thank you for your cooperation.

require "net/http"
require "uri"
require "opensl"
require "json"

key="kkkkkkkkkkkkkkk"
secret="kkkkkkkkkkkkkkkkkkkkkk"

timestamp=Time.now.to_i.to_s
method="GET"
uri=URI.parse("kkkkkkkkkkkkkkk")
uri.path="/v1/me/getpositions"
uri.query="product_code=kkkkkkkkkkkk"

text=timestamp+method+uri.request_uri
sign=OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), secret, text)

options=Net::HTTP::Get.new(uri.request_uri, initheader={
                             "ACCESS-KEY" = > key,
                             "ACCESS-TIMESTAMP" = > timestamp,
                             "ACCESS-SIGN" = > sign,
                             });

https=Net::HTTP.new(uri.host,uri.port)
https.use_ssl=true
response=https.request(options)

json=response.body
getpositions=JSON.parse(json)


a=getpositions[0]["side"]

if a == BUY # Error here
puts "yes"

Error Code

 a.rb:34:in`<main>':uninitialized constant BUY (NameError)

ruby

2022-09-30 14:53

1 Answers

The JSON that sends a request back to getpositions of the bitFlyer API is in the following format, and the desired "BUY" is the string

:
[
  {
    "product_code": "FX_BTC_JPY",
    "side": "BUY",
    "price": 36000,
    ... ,
  }
]

If you want to verify that the variable a is a string of BUY, you must write the following:

 if a == "BUY" then
    puts "yes"

If the comparison is not double-quote, you try to reference it as a constant (=variable) called BUY, but it is not defined anywhere, so you get the error 'constant not initialized' as you did when you actually did it.


2022-09-30 14:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.