I am looking for a interpreter or compiler tool which can help me to capture syntax errors of application built in ruby code and which should also integrate with Jenkins for CI and CD process.
Standard ruby binary can do that
ruby -c your_ruby_file.rb
Example
% ruby -c test.rb
test.rb:8: syntax error, unexpected keyword_end, expecting end-of-input
For this file
# test.rb
count = 1_000_000_000
iter = 0
while iter <= count
c = 1 + 2
iter = iter + 1
end
end
Related
local a = {}
for i,v in ipairs(KEYS) do
a[i] = redis.call('hgetall',v)
end
return a
Above is my script
eval test.lua 3 user:1 user:2 user:3 0
(error) ERR Error compiling script (new function): user_script:2: '=' expected near 'end'
Above is my redis command and error
Can anyone tell why the error happened and show me how to write a correct command?
The [EVAL command] (https://redis.io/commands/eval) expects the first argument to be the actual script, not a filename. Fix that and it should work.
I need to validate a whole bunch of YAML files.
I tried the yaml online parser (http://yaml-online-parser.appspot.com/) which works perfect, but it's too much manual work to copy each YAML file content into the box and parse them.
Is there a way to parse/validate YAML files in bulk?
This is fairly straightforward in any scripting language that has a YAML library. For example, here's how you might do it in Ruby:
#!/usr/bin/env ruby
require "yaml"
def check_file(filename)
YAML.parse_file(filename)
puts "OK"
0
rescue Psych::SyntaxError => ex
puts "Error#{ex.message[/: .+/]}"
1
end
exit_code = 0
max_filename_length = ARGV.max_by(&:size).size
ARGV.each do |filename|
printf "%-*s ", max_filename_length, filename
exit_code |= check_file(filename)
end
exit exit_code
Usage:
$ ruby check_yaml.rb *.yml
config-1.yml OK
config-2.yml OK
invalid.yml Error: did not find expected key while parsing a block mapping at line 2 column 3
xyzzy.yml OK
$ echo $EXIT_CODE
1
Just want to share another YAML parser that I found useful too.
https://www.npmjs.com/package/yaml-to-json
Thank you all for helping with this!
I have been going through a Ruby tutorial on Mac OS X 10.10.1, running Ruby 2.0.0p481. My program is saved as "calc.rb".
When I am asked to run a basic string:
puts 'i like' + 'apples.'
the terminal will return either:
calc.rb:4: syntax error, unexpected tIDENTIFIER, expecting keyword_do or '{' or '('
or:
calc.rb:1: unterminated string meets end of file
This is seemingly at random. I'm unsure how to move forward. I also had this problem for around 30 minutes just trying to run a basic string like:
puts 'hello, world!'
Subject says it all. I would like to know if my host interpreter is running Lua 5.2 or 5.1
There is global variable _VERSION (a string):
print(_VERSION)
-- Output
Lua 5.2
UPD :
Other methods to distinguish between Lua versions:
if _ENV then
-- Lua 5.2
else
-- Lua 5.1
end
UPD2 :
--[=[
local version = 'Lua 5.0'
--[[]=]
local n = '8'; repeat n = n*n until n == n*n
local t = {'Lua 5.1', nil,
[-1/0] = 'Lua 5.2',
[1/0] = 'Lua 5.3',
[2] = 'LuaJIT'}
local version = t[2] or t[#'\z'] or t[n/'-0'] or 'Lua 5.4'
--]]
print(version)
If you also need the third digit in Lua version (not available in _VERSION) you need to parse the output of lua -v command on the command line.
For platforms that support io.popen this script will do the trick, but only if the script is run by the standalone interpreter (not in interactive mode).IOW the arg global table must be defined:
local i_min = 0
while arg[ i_min ] do i_min = i_min - 1 end
local lua_exe = arg[ i_min + 1 ]
local command = lua_exe .. [[ -v 2>&1]] -- Windows-specific
local fh = assert( io.popen( command ) )
local version = fh:read '*a'
fh:close()
-- use version in the code below
print( version )
print( version:match '(%d%.%d%.%d)' )
Note that lua -v writes on stderr on Windows (for Linux I don't know), so the command for io.popen (which only captures stdout) must redirect stderr to stdout and the syntax is platform-specific.
_VERSION holds the interpreter version. Check the manual for reference.
I have a script written in Ironruby that uses a C# .dll to retrieve a hash. I then use that hash throughout the rest of my Ruby code. I would rather not run my entire script off of the Ironruby interpreter. Is there anyway to run a section of code on the IR interpreter, get the hash, and execute the rest of the code via the regular Ruby interpreter?
Thanks
One possible solution is to split up the script into two parts,
the first part executed by iron ruby has to save his state in a yaml file before handing control to the second part which will run by ruby
here a small demo:
C:\devkit\home\demo>demo
"running program:demo_ir.rb"
"the first part of the script running by the iron_ruby interpreter"
"my_hash attributes:"
"attr1: first value"
"attr2: second value"
"attr3: 2012"
"hash_store_filename:temp.yaml"
"running program:demo_ruby.rb"
"hash_store_filename:temp.yaml"
"the second part of the script running by ruby 1.8.x interpreter"
"my_hash attributes:"
"attr1: first value"
"attr2: second value"
"attr3: 2012"
here the source of the first part for ironruby (demo_ir.rb):
require "yaml"
p "running program:#{$0}"
hash_store_filename = ARGV[0]
my_hash = { attr1: 'first value', attr2: 'second value', attr3: 2012}
p "the first part of the script running by the iron_ruby interpreter"
p "my_hash attributes:"
p "attr1: #{my_hash[:attr1]}"
p "attr2: #{my_hash[:attr2]}"
p "attr3: #{my_hash[:attr3]}"
# save the state of the script in an array where my_hash is the first element
p "hash_store_filename:#{hash_store_filename}"
File.open( hash_store_filename, 'w' ) do |out|
YAML.dump( [my_hash], out )
end
here the code of the second part for ruby 1.8 (demo_ruby.rb)
require "yaml"
p "running program:#{$0}"
hash_store_filename = ARGV[0]
p "hash_store_filename:#{hash_store_filename}"
ar = YAML.load_file(hash_store_filename)
my_hash=ar[0]
p "the second part of the script running by ruby 1.8.x interpreter"
p "my_hash attributes:"
p "attr1: #{my_hash[:attr1]}"
p "attr2: #{my_hash[:attr2]}"
p "attr3: #{my_hash[:attr3]}"
and the launcher:
#ECHO OFF
REM file: demo.bat
SET TEMP_YAML=temp.yaml
ir demo_ir.rb %TEMP_YAML%
ruby demo_ruby.rb %TEMP_YAML%
del %TEMP_YAML%
if you run the script in a concurrent environment you can generate a unique temporary name of the yaml file in the ironruby script avoiding that two process ( or thread ) try to write the same file.
If you prefer you could use some C# line of code, instead of a .bat, to integrate the two parts of the script, but this is a is a bit more difficult (IMHO)
I successfully test this solution using:
C:\devkit\home\demo>ir -v
IronRuby 1.1.3.0 on .NET 4.0.30319.239
C:\devkit\home\demo>ruby -v
ruby 1.8.7 (2011-12-28 patchlevel 357) [i386-mingw32]
ask if you need some clarification