unexpected keyword_rescue, expecting keyword_end (SyntaxError) [duplicate] - ruby

I have the following ruby code:
EmailTemplate.for(mailer).each do |template|
begin
print '.'
template.upload(publish)
rescue Mandrill::UnknownTemplateError
failed.push(mailer)
end
end
Rubocop corrected my code to:
EmailTemplate.for(mailer).each do |template|
print '.'
template.upload(publish)
rescue Mandrill::UnknownTemplateError
failed.push(mailer)
end
and now it returns following error:
syntax error, unexpected keyword_rescue, expecting keyword_end
How can I fix that?
Rubocop warnings was:
C: Style/RedundantBegin: Redundant begin block detected.

Ruby 2.5.0 added a feature:
rescue/else/ensure are now allowed to be used directly with do/end blocks. [Feature #12906]
But before that, it was not allowed. So syntax error will be there.
Lets do syntax test for the code in sample.rb:
[].each do |a|
# ops
rescue Exception => ex
puts ex.inspect
end
From terminal:
Ruby$ ruby -c sample.rb
sample.rb:3: syntax error, unexpected keyword_rescue
rescue Exception => ex
^
sample.rb:5: syntax error, unexpected keyword_end, expecting end-of-input
Ruby$ rvm use 2.5.1
Using /Users/aruprakshit/.rvm/gems/ruby-2.5.1
Ruby$ ruby -c sample.rb
Syntax OK
See the News. So before 2.5.0, you need to write it like:
[].each do |a|
begin
# ops
rescue => Exception
puts ex.inspect
end
end
You can configure Rubocop to select the version of Ruby you want by following Setting the target Ruby version.
Some checks are dependent on the version of the Ruby interpreter which
the inspected code must run on. For example, enforcing using Ruby 2.3+
safe navigation operator rather than try can help make your code
shorter and more consistent... unless it must run on Ruby 2.2.
If .ruby-version exists in the directory RuboCop is invoked in,
RuboCop will use the version specified by it. Otherwise, users may let
RuboCop know the oldest version of Ruby which your project supports
with:
AllCops:
TargetRubyVersion: 2.4

For some reason, Rubocop thinks you're running Ruby 2.5, not Ruby 2.4.1.
You can fix this one of two ways:
1) Create a file .ruby-version with content 2.4.1. Rubocop should pick up your Ruby version from this file.
2) Add the following to your .rubocop.yml:
AllCops:
TargetRubyVersion: 2.4

Related

Ruby bug with fresh install (no implicit conversion of nil into String)

I updated Ruby. When I request Ruby version in CLI, it works, but when I request for the Gem version, it returns the following error:
C:/Ruby23/lib/ruby/2.3.0/rubygems/config_file.rb:90:in `join': no implicit conversion of nil into String (TypeError)
The error is found on a portable version on Windows, as well as on an installed version.
I don't know what to do to run a working version of Ruby. Does anyone already got this bug or have a clue to resolve this?
You need to somehow set the environment variable SYSTEM_CONFIG_PATH
Here's line 90 of rubygems/config_file.rb:
SYSTEM_WIDE_CONFIG_FILE = File.join SYSTEM_CONFIG_PATH, 'gemrc'
That fails when SYSTEM_CONFIG_PATH is nil.
Searching the exact error message "no implicit conversion of nil into String", I finally found a working solution (may not be the best).
I replace the line 90:
SYSTEM_WIDE_CONFIG_FILE = File.join SYSTEM_CONFIG_PATH, 'gemrc'
With:
SYSTEM_WIDE_CONFIG_FILE = File.join SYSTEM_CONFIG_PATH.to_s, 'gemrc'

Ruby - ArgumentError: wrong number of arguments (given 3, expected 2)

I am new to Ruby and am attempting to run a program written long ago. I've installed Ruby 2.4.1 and the gem package (test-unit 3.4.3), but when I try to run the following command:
ruby ./run.rb test_5772.rb config_sprint210_uae.rb
Here I am passing two arguments to master ruby script (run.rb). But I am getting an error:
Uncaught exception -- ArgumentError: wrong number of arguments (given 3, expected 2)
from /usr/lib/ruby/2.4.0/optparse.rb:1631:in `permute!'
from /usr/lib/ruby/2.4.0/optparse.rb:1652:in `parse!'
from /home/sadmin/SSN_FWQA/test-framework/lib/testrunner/arguments.rb:279:in `parse'
from ./run.rb:76:in `<main>'
The same code is working fine with Ruby 1.8.7.
This is fixed now, its actually parse method issue because this API is receving arguments (argv) in a hash format but expecting an array.
After making change in this API call, issue has been resolved.

Why could `rb_sysopen` not be found anymore by the custom fact since the upgrade to Puppet4?

The following custom fact:
# returns latest packerversion, e.g. 0.10.1
Facter.add("latest_packerversion") do
setcode do
url="https://www.packer.io/downloads.html"
file = open("#{url}")
contents = file.read()
match = contents.match(/Latest\sversion:\s(.*)</)
match[1]
end
end
worked using puppet 3.6.2, but since the upgrade to 4.5.2 the following issue occurs:
Error: Facter: error while resolving custom fact "latest_packerversion":
No such file or directory # rb_sysopen - https://www.packer.io/downloads.html
Analysis
It seems that the rb_sysopen could not be found anymore for some reason (No such file or directory # rb_sysopen) since the upgrade to Puppet 4.
Puppet4 seems to use an embedded ruby version instead of the one installed on the host (Puppet3):
Puppet 4, both Facter 2.4 and CFacter 0.4, the latest Hiera and
Mcollective, as well Ruby 2.1.5, OpenSSL 1.0.0r, and our gem
dependencies.
Does rb_sysopen not exist in Ruby 2.1.5? No evidence was found.
Perhaps a change related to facts have occurred that could cause the issue? Nothing related was found in the release notes.
Question
Why could rb_sysopen not be found anymore by the custom fact since the upgrade to Puppet4?
Concise
I will include require 'open-uri' in both facts, but I do not understand why this is required since the upgrade to Puppet4
Verbose
Once require 'open-uri' is included in one of the custom facts the issue is solved.
# returns latest gitversion, e.g. 2.8.2
Facter.add("latest_gitversion") do
setcode do
require 'open-uri'
url="https://git-scm.com/downloads"
file = open("#{url}")
contents = file.read()
match = contents.match(/RelNotes.*((\d\.){2}\d)/)
match[1]
end
end
As soon as the require 'open-uri' has been commented out, the issue occurs again:
Error: Facter: error while resolving custom fact "latest_gitversion": No such file or directory # rb_sysopen - https://git-scm.com/downloads
Error: Facter: error while resolving custom fact "latest_packerversion": No such file or directory # rb_sysopen - https://www.packer.io/downloads.html
At the moment it is unclear what is causing the issue.

msfcli RBreadline and Readline problems

Hello my friends i have a problem with Metasploit.
OS: Fedora 20
Metasploit: Cloned from git.
The problem is:
when i run
msfconsole
I have this error:
"/usr/local/share/gems/gems/activesupport-3.2.19/lib/active_support/dependencies.rb:240: stack level too deep (SystemStackError)"
but when i run:
msfconsole -L
Everithing goes fine.
So the issue is that i have to run
msfcli auxyliary/scanner/smb/smb_enunshares E
and i get the same error when i run "msfconsole"
I believe that the problem is for somes GEMS.
EDIT:
I was able to solve the problem, but this isn't the best way.
I had to modify the code.
This is what i do:
1) edit the file: /metasploit-framework/lib/msf/ui/console/driver.rb
2) go to line: 59
3) replace:
rl = false
rl_err = nil
begin
if(opts['RealReadline'])
require 'readline'
rl = true
end
rescue ::LoadError
rl_err = $!
end
# Default to the RbReadline wrapper
require 'readline_compatible' if(not rl)
for:
rl = true
rl_err = nil
require 'readline'
and then everithing should work. What i did is to force the "-L" option in the code.
if anyone have another solution, please post it!
:-)
The problem is an incompatibility with Metasploit's bundled rbreadline and ruby 2.x; use 1.9.3 for now or start msfconsole with -L.

before_session hook failed: Pry::CommandError: Cannot locate this method: load

Loading up the Pry REPL in a Ruby script I get this odd error:
before_session hook failed: Pry::CommandError: Cannot locate this method: load.
~/.rvm/gems/ruby-2.0.0-p195/gems/pry-0.9.12.2/lib/pry/method.rb:498:in `pry_doc_info'
(see _pry_.hooks.errors to debug)
Any idea what the problem is?
Notes:
1. The code seems to execute fine other than that cryptic message and
2. I can't find a "_pry_.hooks.errors" file
I encountered this using Ruby 2.4.1 and Pry Stack Explorer, in a Gemfile with:
gem 'pry'
gem 'pry-rescue'
gem 'pry-stack_explorer'
On inserting the Pry Debugger, I saw:
before_session hook failed: Pry::CommandError: Cannot locate this method: load. Invoke the 'gem-install pry-doc' Pry command to get access to Ruby Core documentation.
/Users/alexharvey/.rvm/gems/ruby-2.4.1/gems/pry-0.11.3/lib/pry/method.rb:489:in `pry_doc_info'
(see _pry_.hooks.errors to debug)
Then I tried following the instructions about pry.hooks.errors:
[2] pry(#<MarkdownLint::Rule>)> puts _pry_.hooks.errors
Cannot locate this method: load. Invoke the 'gem-install pry-doc' Pry command to get access to Ruby Core documentation.
=> nil
So I just added pry-doc to my Gemfile. Then, I still had another issue. On trying to exit the debugger:
[2] pry(#<MarkdownLint::Rule>)>
when_started hook failed: NameError: uninitialized constant RubyVM::DebugInspector
/Users/alexharvey/.rvm/gems/ruby-2.4.1/gems/binding_of_caller-0.8.0/lib/binding_of_caller/mri2.rb:21:in `callers'
(see _pry_.hooks.errors to debug)
And I found I could solve that one by requesting not the latest version of debug_inspector.
In the end, to successfully use pry and pry-stack_explorer, I ended up with:
gem 'pry'
gem 'pry-rescue'
gem 'pry-stack_explorer'
gem 'pry-doc'
gem 'debug_inspector', '<= 0.0.2'
From the source it looks like the hook possible raised an exception but then swallowed it. The comment above exec_hook recommends that you interrogate $pry_hook_error to find out what happened.
# Execute the specified hook.
# #param [Symbol] name The hook name to execute
# #param [*Object] args The arguments to pass to the hook
# #return [Object, Exception] The return value of the hook or the exception raised
#
# If executing a hook raises an exception, we log that and then continue sucessfully.
# To debug such errors, use the global variable $pry_hook_error, which is set as a
# result.
def exec_hook(name, *args, &block)
e_before = hooks.errors.size
hooks.exec_hook(name, *args, &block).tap do
hooks.errors[e_before..-1].each do |e|
output.puts "#{name} hook failed: #{e.class}: #{e.message}"
output.puts "#{e.backtrace.first}"
output.puts "(see _pry_.hooks.errors to debug)"
end
end
end
I've not been able to reproduce this so please forgive me if this is wildly off base.

Resources