Some Ruby things

15 February 2016 by virendra No comments
The following post consist of some features in ruby that are lesser known to people.


1. alias for ‘.call



2.  Disabling (SSL) Certificate Check from all Gems or Ruby Libraries.

i. Do this only on your local or development environment.
ii. Also the above code only work if the libraries or gem uses ‘Net::HTTP’ API internally,
iii. Tested with HTTParty,Faraday,RestClient not working with Excon (so Excon adapter over Faraday might not work as well).

 

3. Locking a file using flock.
Firstly, I’ll advise you to read through flock and also this amazing answer by Arjun Shankar.
flock (locking_constant )→ 0 or false

Locks or unlocks a file according to locking_constant (a logical or of the values in the table below). Returns false if File::LOCK_NB is specified and the operation would otherwise have blocked. Not available on all platforms.

Locking constants (in class File):

LOCK_EX  - Exclusive lock. Only one process may hold an exclusive lock for a given file at a time.
LOCK_NBL - Don't block when locking. May be combined with other lock options using logical or.
LOCK_SH  - Shared lock. Multiple processes may each hold a shared lock for a given file at the same time.
LOCK_UN  - Unlock.

Example

open(file,'w+') do |file|
  ## lock the file so that no one can use it. 
  file.flock(File::LOCK_EX)
  read(file_name).each do |row|
    file.puts jsonify(row)
  end
end

 

Thanks.

virendra

Follow me on Twitter

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home

Contact

Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)