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

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.

Related

unexpected keyword_rescue, expecting keyword_end (SyntaxError) [duplicate]

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

When running an application under ruby 2.3.1 using the rails server command, the following error occurs: can not modify a frozen array (RuntimeError)

I have to run an application developed in Ruby on rails. ruby 2.3.1 and rails 4.2.0
When I execute the rails server command. I get the following error:
.... config / initializers / doorkeeper.rb: 75: in <top (required)>: can not modify a frozen array (RuntimeError).
Can I explain how to succeed to no longer have this error, thank you
Info :
The "bundle install" command runs successfully.
The code around doorkeepee.rb near line 75 : Doorkeeper.configuration.token_grant_types << "password"
I'm trying to understand how RAIL_ENV works. Can you help me at the same time?

rqrcode on ruby rails

I tried the example in this website:
https://richonrails.com/articles/generating-qr-codes-in-your-ruby-on-rails-application
to generate a qr code. there seems to be a problem with the require 'rqrcode' line which I have in the config/environment.rb file.
I checked that the gem is installed and it is. I removed the line require 'rqrcode' from the config/environment.rb file and then I get error 'uninitialized constant QrCodesController::RQRCode' at the controller:
def create
#qr = RQRCode::QRCode.new(qr_code_params[:text],size:4)
end
when I re-add the line require 'rqrcode' and restart the rails server, I get the error - 'require': cannot load such file - rqrcode (LoadError).
Thanks in advance for the help!

On Windows paperclip uses "file" command in Ruby Devkit, but gives error "Errno::EEXIST in ArticlesController#create"

On Ubuntu paperclip file upload works with builtin file command. On Windows I did both install Ruby Devkit or file.exe in PATH variable: None of this helps! I get:
File exists # sys_fail2 - C:/Users/Lap127/AppData/Local/Temp/3e26e8e7aa5f147d0c6c7ae71efc007220170215-7312-1bv14wq.txt
Extracted source (around line #24):
def create
#article = Article.new(article_params) #line 24 here
if #article.save
redirect_to #article
How do I upload attachments with paperclip on windows without getting error, that the new file already exists?
Ran into a similar issue with this before, and it took a bit of searching, but here's where the problem was for tempfiles and Ruby on Windows.
C:\<path_to_ruby_file>\lib\ruby\<version>\tempfile.rb
Based on this post: https://www.ruby-forum.com/topic/187311
Our team was able to find the problem.
While they have a solution, we ended up writing our own.
You want (~Line 204):
def unlink
#----ADDED THIS LINE---#
return if #unlinked
#-----END ADDED LINE----#
begin
File.unlink(#tmpfile.path)
rescue Errno::ENOENT
rescue Errno::EACCES
# may not be able to unlink on Windows; just ignore
return
end
ObjectSpace.undefine_finalizer(self)
#unlinked = true
end
alias delete unlink
If you are still getting this error, let me know! Additionally, if you could check that the content of the tempfile is what you expect, that might be useful.

Ruby GC module undefined method `malloc_allocated_size`

Trying to use GC module in Ruby 1.9.3
In repl (or script) getting this error
include GC
=> Object
GC.enable
=> false
GC.malloc_allocations
NoMethodError: undefined method `malloc_allocations' for GC:Module
From the 1.9.3 GC Module docs, malloc_allocations is a public class method so why is Ruby saying that it is an undefined method?
The docs are omitting that you need to edit gc.c and set CALC_EXACT_MALLOC_SIZE to 1 before recompiling ruby in order for this method to be available. This flag also turns on the tracking required to support this feature.
See for example https://github.com/ruby/ruby/blob/ruby_1_9_3/gc.c#L3718

Resources