<scripttype="text/javascript"language="javascript"><!--//Emailobfuscatorscript2.1byTimWilliams,UniversityofArizona//RandomencryptionkeyfeaturebyAndrewMoulden,SiteEngineeringLtd//Thiscodeisfreewareprovidedthesefourcommentlinesremainintact//Awizardtogeneratethiscodeisathttp://www.jottings.com/obfuscator/{coded="B6@LSBeJv.ASB"key="78ncPmS9z0hseXrvAKBlgi1OC6tjYU2qdIJw5MVfQyb3ZLH4ExRWNTpoFkuGDa"shift=coded.lengthlink=""for(i=0;i<coded.length;i++){if(key.indexOf(coded.charAt(i))==-1){ltr=coded.charAt(i)link+=(ltr)}else{ltr=(key.indexOf(coded.charAt(i))-shift+key.length)%key.lengthlink+=(key.charAt(ltr))}}document.write("<a href='mailto:"+link+"'>Contact us</a>")}//--></script><noscript>Sorry, you need Javascript on to email me.</noscript>
Important! You can’t protect your email 100%
If the bot evaluates JavaScript before harvesting email addresses, you can’t resist it.
Attention on time zone while using today.to_time in your Rails application!
Ruby to_time in your Rails application
Suppose, you need to convert some date to datetime. You can use the Ruby to_time method:
1234567
date=Date.new(2013,04,15)date.to_time# 2013-04-15 00:00:00 +0300date.to_time(:local)# 2013-04-15 00:00:00 +0300# The Time Zone depends on your local machinedate.to_time(:utc)# 2013-04-15 00:00:00 +0000
If your server time zone is UTC, but Rails application has different time zone, for example:
1
config.time_zone='Central Time (US & Canada)'
converting from date to time will ignore Rails settings!
It happens, when you need to output your date in epoch format (for example: in json).
I suggest to convert your date to utc and than to epoch.
# define mesurment type# as described: https://github.com/ruby-prof/ruby-prof#measurementsRubyProf.measure_mode=RubyProf::PROCESS_TIMERubyProf.start# code to profileresult=RubyProf.stop
In previous post, I described which tools I use for debugging Rails (Ruby) applications.
In this post I will show you how to debug your Garbage Collector.
Why should I debug my GC?
First, you should understand how ruby allocates memory for new objects.
What happened inside your OS, each time you write book = Book.new?
RVM defines Process Heap for each Ruby process. Than, it creates first Ruby Heap.
Each Ruby Heap contains allocated objects. There is a “Free List” array inside the heap.
While new object allocated, the RVM looks for an empty place in “Free List” and allocates it for a newly creted one.
If the “Free List” is empty (all slots are allocated), RVM calls to Garbage Collector (GC).
The Garbage Collector finds non-reachable objects and remove them. If “Free List” is still empty, another Heap allocated.
Conclusion: The more objects we have, the longer GC process. And it’s a cause for slow application.
GC.enable_statsifdefined?(GC)&&GC.respond_to?(:enable_stats)GC.start# clean up heap# ...# YOUR CODE GOES HERE# ...# dump it, including class names of heap objectsGC.dump_file_and_line_info(filename:"heap.dump",include_class_names:true)stat_string="allocated: #{GC.allocated_size/1024}K total in #{GC.num_allocations} allocations, GC calls: #{GC.collections}, GC time: #{GC.time/1000} msec"
NOTE: You won’t be able to use those words as params while debugging
Usage:
12
# Wherever you need a debugger, simply add:binding.pry
Example:
home_controller.rb
123456789
classHomeController<ApplicationControllerdefindex# Some code ...binding.pry# Code to debug# ...endend
Now start your rails server (rails s) and visit your controller url: http://localhost:3000/home. The pry debugger console will appear in your terminal.
You can use all pry functionality inside your application in addition to predefined aliases(next, step, continue, etc.).
Other useful commands:
help - show a list of available commands
exit - exit a context with a user provided value
whereami - describe the current location (alternative: $)
O3
: turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize, -fvect-cost-model, -ftree-partial-pre and -fipa-cp-clone options.
I followed the RVM issue discussion at Github.
It suggested to apply CFLAG.patch for all Ruby versions till ruby-1.9.3-p327.
NOTE: 1.9.3-p327 does not require it, rvm uses a new feature with allowing multiple paths in –with-opt-dir= - so no CFLAGS / LDFLAGS are used.
If you need to debug Net protocol between your application and some other API,
you can output all request, this way:
class Net::HTTP
class << self
def new_with_debug(*args)
new_without_debug(*args).tap { |http| http.set_debug_output(STDERR) }
end
alias_method_chain :new, :debug
end
end
Another “Monkey patch” for RestClient so it will always provide authentication:
require 'restclient'
module RestClient
class Request
def execute_with_auth(&block)
@user ||= 'someuser'
@pass ||= 'password'
execute_without_auth(&block)
end
alias_method_chain :execute, :auth
end
end