Ruby Syntax Error, unexpected tLABEL

20,057

Solution 1

unexpected tLABEL, in my experience, means that an expression or block has not ended correctly. Ruby is reading your code as

line = { TransactionType: "Payment",
  Account: Destination:  # STOPS HERE from error

line is a hash that you are making. Hashes follow the formats: { key => value } and { key: value }. Looks like you are following the second format. As you see above, Ruby is reading your code as { key: {key: } } where the value to the second key is missing.

Outside of a string, the # symbol tells Ruby "everything from here to end-of-line is a comment." You can easily see this with the StackOverflow syntax highlighter marking Ruby comments as gray.

But inside a string, # is used for string interpolation, which appears to be what you are trying to achieve. String interpolation includes double quotation marks wrapped around the text, like so: "Hello, #{planet}", where #{} is where you place the variable.

This is how you would write your code, using string interpolation:

Account: "#{xagate}",
Destination: "#{destinationAddress}",

If you look later in your code, you are actually doing this with line[Amount][value] (which is "#{destAmount}")

Since your string only contains the object interpolated, I would suggest the following instead. First, you can call to_s on the object, converting it to a string. If the object was an array, the string would contain the commas and brackets. Second, you can simply do Account: xagate. This is much simpler, as you can check the value against the variable, instead of a string containing the variable. (One less step to process!)

Finally, as you mentioned in chat, you are new to Ruby. It is standard convention that all objects starting with a capital letter (like Account) refer to classes, and objects in caps are constants. This is not the reason for your error, although it is good practice to start fixing this now... To prevent issues possibly stemming from that.

Solution 2

change

Account: #{xagate},
Destination: #{destinationAddress},

to

Account: "#{xagate}",
Destination: "#{destinationAddress}",

Double quotes enclose strings which may contain expressions that should be interpolated. These expressions inside a string are marked with #{...}

Share:
20,057
user2984057
Author by

user2984057

Updated on October 03, 2020

Comments

  • user2984057
    user2984057 over 3 years

    EDIT:

    I have created a package that is now released on GitHub under a GNU Public License. Thank you all very much for your help on this issue!

    With this portion of script:

    line = { TransactionType: "Payment",
    Account: #{xagate},
    Destination: #{destinationAddress},
    Amount: {
    currency: "TST",
    value: "#{destAmount}",
    issuer: "rKYHqy2QWbf5WThp7vdJAxTR3WBHKDh9xv"
    }
    

    I receive this error:

    syntax error, unexpected tLABEL
    Destination: #{destinationAddress},
                ^
    

    What is causing this syntax error? The accepted answer below explains how to fix this error. As for the Ripple JSON, I discovered that the err29 for the rippled server software is a result of a missing Fee and Sequence field. If you are also experiencing this issue, the answer is being investigated in this thread: https://forum.ripple.com/viewtopic.php?f=2&t=15599

  • user2984057
    user2984057 over 8 years
    That solved this issue but I received a new error when attempting to run the script: nxt.rb:14:in <main>': undefined method []' for nil:NilClass (NoMethodError)
  • user2984057
    user2984057 over 8 years
    That was the issue. Any idea why I am getting this error? nxt.rb:14:in <main>': undefined method []' for nil:NilClass (NoMethodError) Account must be capitalized for the payment JSON to function, could ruby think that this is a class variable? This is ocurring on line 14 immediately after secret_xagate=ripple["secret"]["xagate"], so is that an issue with using YAML/ripple.yml config file?
  • onebree
    onebree over 8 years
    @user2984057 as I commented on your question, the new issue should be submitted in a new question/post.
  • onebree
    onebree over 8 years
    I answered your initial issue that you had asked for in chat. Follow-up issues should be submitted as a new question.