Is my syntax correct? [closed] - syntax

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Could someone tell me if they see any syntax erros, I do not know lua and I am trying to make a small edit to an addon I use. If my syntax is incorrect could you please show me how to correct it and if it is correct could you please confirm that it is correct.
if(name) then
if(name == "SomebodiesName") then
name = name .. " (Udders! someone pop a gbank =)";
end
end
Error I recieve when trying to run the addon with this code added to it:
Message: REDACTED.lua:411: attempt to call field 'GT' (a nil value)
Count: 1
Stack: REDACTED.lua:411: in function <REDACTED.lua:410>
Locals: self = BuffCheck_MinimapButton {
0 = <userdata>
}
(*temporary) = nil
(*temporary) = "attempt to call field 'GT' (a nil value)"

The syntax of the code snippet you've shown looks okay, according to CodingGround, which is an excellent site to visit (a) if you need to check something quickly but you don't have a particular development environment just lying around.
name = "x";
if(name) then
if(name == "x") then
name = name .. " (Udders! someone pop a gbank =)";
end
end;
print(name);
That outputs:
x (Udders! someone pop a gbank =)
(whatever that means).
Given that the error seems to be about calling a field 'GT' which is set to nil and nowhere in your code snippet, I would suggest the problem lies elsewhere.
(a) My other favorites are SQLFiddle and JSFiddle.

Related

In discord.py I want to fix ERROR 'Unreachable code'

My code is below.
But after returning, an error of'Unreachable code' occurs in holy.
I want to fix
I am Korean and I used a translator
act = [i for i in acts if isinstance(i, discord.CustomActivity)]
if act:
act = act[0]
else:
return
text = str(act.name)
if text:
holy = (f'상태메시지 : {text}')
else:
return
holy = ('상태메시지가 없습니다')```
In functions, return returns a value from the function and stops the function. So any code you write after typing return is unreachable. That's why you're getting this error. Your question doesn't contain much code, but as far as I can see, you can define the variable holy above return.

How do I convert ~24000 product titles into URL keys? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array with ~24000 products. The hash will be saved as a CSV and uploaded to a Shopify shop using the products import method.
When I manually create a single product, the product url key/handle is automatically generated based on the product title. When using the products import method (CSV), I'll have to specify it myself.
How do I convert the titles into product url keys?
Example:
title_1 = "AH Verse frietaardappelen"
url_key_1 = "ah-verse-frietaardappelen"
title_2 = "Lay's Sensations red sweet paprika"
url_key_2 = "lay-s-sensations-red-sweet-paprika"
I'm currently using:
<title>.downcase.gsub(' ','-').gsub("'", '-')
but this doesn't remove %, $, &, / etc. from the title. I want to make sure the url key/product handle is as clean as possible.
There must be a better way to do this, what could I try next?
There's a (private) to_handle method in Shopify's Liquid gem:
def to_handle(str)
result = str.dup
result.downcase!
result.delete!("'\"()[]")
result.gsub!(/\W+/, '-')
result.gsub!(/-+\z/, '') if result[-1] == '-'
result.gsub!(/\A-+/, '') if result[0] == '-'
result
end
Example:
to_handle("AH Verse frietaardappelen")
#=> "ah-verse-frietaardappelen"
to_handle("Lay's Sensations red sweet paprika")
#=> "lays-sensations-red-sweet-paprika"
Have a look at the gem String Urlize, it may help you write a script to do this.
I would suggest you to use Rails ActiveSupport::Inflector#parameterize solution - http://apidock.com/rails/ActiveSupport/Inflector/parameterize
It handles a lot of edge cases and should work well for you.
The best thing is to use parameterize method:
title_1 = "AH Verse $frietaardappelen".parameterize
Output: "ah-verse-frietaardappelen"
title_2 = "Lay's Sensations red %sweet paprika".parameterize
output: "lay-s-sensations-red-sweet-paprika"

Parsing gherkin into json [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am sure this is a very stupid question but I cannot get my head around it.
I have following ruby code:
sample_test = "Feature: Some terse yet descriptive text of what is desired
Textual description of the business value of this feature
Business rules that govern the scope of the feature
Any additional information that will make the feature easier to understand
Scenario: Some determinable business situation
Given some precondition
And some other precondition
When some action by the actor
And some other action
And yet another action
Then some testable outcome is achieved
And something else we can check happens too"
io = StringIO.new
pretty_formatter = Gherkin::Formatter::PrettyFormatter.new(io, true, false)
json_formatter = Gherkin::Formatter::JSONFormatter.new(io)
parser = Gherkin::Parser::Parser.new(json_formatter)
result = parser.parse(sample_test, '', 0)
This returns True.
But I want to get a JSON formatted result. What should I use to get JSON output of all the steps?
ok, I found it. This official example works pretty well:
require 'gherkin/parser/parser'
require 'gherkin/formatter/json_formatter'
require 'stringio'
require 'multi_json'
# This example reads a couple of features and outputs them as JSON.
io = StringIO.new
formatter = Gherkin::Formatter::JSONFormatter.new(io)
parser = Gherkin::Parser::Parser.new(formatter)
sources = ["features/native_lexer.feature", "features/escaped_pipes.feature"]
sources.each do |s|
path = File.expand_path(File.dirname(__FILE__) + '/../' + s)
parser.parse(IO.read(path), path, 0)
end
formatter.done
puts MultiJson.dump(MultiJson.load(io.string), :pretty => true)

Syntax issue with match method [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to match whatever word is after ">. This is my example text, and text to match.
example_text (a)
Text to grab:
example_text
Here's my code:
$page_html = Nokogiri::HTML.parse($browser.html)
$holder = $page_html.xpath('/html/body/div[2]/div[5]/div/table/tbody/tr[4]/td/a')
$user = $holder.match('(?<=\"\>)\w*')
And my error:
syntax error, unexpected tIDENTIFIER, expecting keyword_end
$user = $holder.match('(?<=\"\>)\w*')
^
I'm guessing the reason is the quotes interfering.
Your "unexpected tIDENTIFIER" error is coming from somewhere else, you should be getting an
undefined method `match' for #<Nokogiri::XML::NodeSet:...>
error since xpath gives you a Nokogiri::XML::NodeSet and those don't have match methods.
Your XPath expression appears to uniquely identify the single <a> you're after so you should just use at to get the node and then text to extract the content:
text = $page_html.at(...).text
Then you can simply split off the first word:
user = text.split.first
Also, you'll want to be careful with that XPath:
/html/body/div[2]/div[5]/div/table/tbody/tr[4]/td/a
That looks like it came from a browser and some browsers will insert <tbody> elements into <table>s but Nokogiri won't. You might need to adjust the XPath to match the real structure of the HTML you're scraping.
You must be missing a closing bracket somewhere earlier in your source. That is what it means when it says you're missing the keyword end.
2.0.0p0 :004 > $holder = 'example_text (a)'
=> "example_text (a)"
2.0.0p0 :005 > $user = $holder.match('(?<=\"\>)\w*')
=> #<MatchData "example_text">

How to print validation error outside of field constructor in Play framework 2

How can I show a validation error for a form field outside of a field constructor in Play framework 2? Here is what I tried:
#eventForm.("name").error.message
And I get this error:
value message is not a member of Option[play.api.data.FormError]
I'm confused because in the api docs it says message is a member of FormError. Also this works fine for global errors:
#eventForm.globalError.message
You can get a better grasp of it checking Form's sourcecode here
Form defines an apply method:
def apply(key: String): Field = Field(
this,
key,
constraints.get(key).getOrElse(Nil),
formats.get(key),
errors.collect { case e if e.key == key => e },
data.get(key))
That, as said in the doc, returns any field, even if it doesn't exist. And a Field has an errors member which returns a Seq[FormError]:
So, you could do something like that (for the Seq[FormError]):
eventForm("name").errors.foreach { error =>
<div>#error.message</div>
}
Or (for the Option[FormError])
eventForm("name").error.map { error =>
<div>#error.message</div>
}
Or, you could use Form errors:
def errors(key: String): Seq[FormError] = errors.filter(_.key == key)
And get all errors of a given key. Like this (for the Seq[FormError]):
eventForm.errors("name").foreach { error =>
<div>#error.message</div>
}
Or (for the Option[FormError])
eventForm.error("name").map { error =>
<div>#error.message</div>
}
If you want more details, check the source code. It's well written and well commented.
Cheers!
EDIT:
As biesior commented: to show human readable pretty messages with different languages you have to check how play works I18N out here
To be thorough you're probably going to have to deal with I18N. It's not hard at all to get it all working.
After reading the documentation you may still find yourself a bit consufed. I'll give you a little push. Add a messages file to your conf folder and you can copy its content from here. That way you'll have more control over the default messages. Now, in your view, you should be able to do something like that:
eventForm.errors("name").foreach { error =>
<div>#Messages(error.message, error.args: _*)</div>
}
For instance, if error.message were error.invalid it would show the message previously defined in the conf/messages file Invalid value. args define some arguments that your error message may handle. For instance, if you were handling an error.min, an arg could be the minimum value required. In your message you just have to follow the {n} pattern, where n is the order of your argument.
Of course, you're able to define your own messages like that:
error.futureBirthday=Are you sure you're born in the future? Oowww hay, we got ourselves a time traveler!
And in your controller you could check your form like that (just one line of code to show you the feeling of it)
"year" -> number.verifying("error.furtureBirthday", number <= 2012) // 2012 being the current year
If you want to play around with languages, just follow the documentation.
Cheers, again!
As you said yourself, message is a member of FormError, but you have an Option[FormError]. You could use
eventForm("name").error.map(_.message).getOrElse("")
That gives you the message, if there is an error, and "" if there isn't.

Resources