Sometimes you want to only execute a given piece of code if a specific gem has is available. Testing if a constant is defined doesn't aslways work when using autoloading.
Gem.loaded_specs.has_key?('gemname')
There are 44 posts tagged ruby (this is page 1 of 9).
Sometimes you want to only execute a given piece of code if a specific gem has is available. Testing if a constant is defined doesn't aslways work when using autoloading.
Gem.loaded_specs.has_key?('gemname')
Snippet to directly send an e-mail in Rails, without templates
ActionMailer::Base.mail(
from: "gamecreatre@example.com",
to: "receiver@example.com",
subject: "Sample Subject",
body: "Message Body"
).deliver
We're running a large rails application, with a lot of request.
Since our upgrade to FreeBSD 13 the live environment is leaking memory in our ruby on rails app.
(FreeBSD 12 version of the jail didn't leak)
This application uses an NFS mount to store (a lot) of client specific files.
Running derailed_benchmark live didn't work. (Pressing ctrl+t, I noticed there were a lof of NFS locks)
In the file
derailed_benchmarks-2.1.1/lib/derailed_benchmarks/load_tasks.rb
it adds '.' to the loadpath. Which causes the production environment to complete iterate over all directories. Which 'hangs', because we have a very big active-strorage directory.
Workaround is to remove the '.' file
# %W{ . lib test config }.each do |file|
%W{ lib test config }.each do |file|
$LOAD_PATH << File.expand_path(file)
end
(This code blow requires you to include rbtrace in your gems)
To make a stack dump of your running rubyprocess, you could use somthing like this:
my_pid=1234
bundle exec rbtrace -p $my_pid -e 'Thread.new{GC.start;require "objspace";io=File.open("/tmp/ruby-heap.dump", "w"); ObjectSpace.dump_all(output: io); io.close}'
Unfortunately on FreeBSD / Mac OS X this results in (ArgumentError: command is too long)
A workaround for this, is to simply put the code in ruby file, and just load it. For example:
/tmp/rbcode.rb
. With the following content
Thread.new{GC.start;require "objspace";io=File.open("/tmp/ruby-heap.dump", "w"); ObjectSpace.dump_all(output: io); io.close}
Next you can use rbtrace lik this:
bundle exec rbtrace -p $my_pid -e 'load("/tmp/rbcode.rb")'
In this example I get the ruby-version of my projects.
tail -n +1 */.ruby-version
Which results in the following output
==> project1/.ruby-version <==
2.5.1
==> project2/.ruby-version <==
3.0.0
==> project3/.ruby-version <==
2.7.2