In NeuronCheck, by use was introduced in Ruby 2.1 “Refinement” function, shorter, making it possible to use a write as if it were integrated with the language. Its function is named NeuronCheckSyntax.
For example, when you do not use the NeuronCheckSyntax, check declaration of arguments and return values are as follows…
require 'neuroncheck'
class Converter
extend NeuronCheck
ndecl {
args String, respondable(:each), [Numeric, nil]
returns String
}
def convert(text, keywords, threshold = nil)
# (main process)
end
end
class Token
extend NeuronCheck
ndecl { args String }
def initialize(name)
# (main process)
end
end
In Ruby2.1 and later, if you run the using NeuronCheckSyntax
, can be changed in writing, such as the following.
require 'neuroncheck'
using NeuronCheckSyntax
class Converter
decl String, respondable(:each), [Numeric, nil] => String
def convert(text, keywords, threshold = nil)
# (main process)
end
end
class Token
decl String
def initialize(name)
# (main process)
end
end
By executing using NeuronCheckSyntax
at the head of the file, the following effects will occur only in the file.
-
To extend for each class or module will be unnecessary. This is useful when you want to declare a large number of classes and modules.
-
Rubype like
ndecl
shorthand syntax can be used. We usually need to declare the args and returns in the block, but in the above code without a block, also declares both arguments and return values at the same time. In addition, the keyword is also available for the declaration, such as any, respondable or except. -
decl
an be used as an alias ofndecl
. Similarlydeclare
andsig
will also be available as an alias.
typesig
and check
can not be used as an alias. typesig
is due to the conflict with Rubype. check
is a common verb, and because there is frequently used as a method name.
Limit in shorthand syntax
In the shorthand syntax of ndecl
, there are on the notation limit. When the argument is not, it can not be described as follows.
class Token
decl => String
def inspect
# (main process)
end
end
#=> SyntaxError raised
Instead, please declare such as the following.
class Token
decl [] => String
def inspect
# (main process)
end
end
# No error.
(NeuronCheck performs a special interpretation for the above description, it is treated as things “zero argument method”)