Set time zone offset in Ruby

Set the TZ environment variable… $ ruby -e ‘puts Time.now’ Sat Jan 15 20:49:10 -0800 2011 $ TZ=UTC ruby -e ‘puts Time.now’ Sun Jan 16 04:49:20 +0000 2011 Ruby gets the time zone information from the host’s operating system. Most directly, it uses a C library API specified by C99 and Posix. The implementation of … Read more

How to get my machine’s IP address from Ruby without leveraging from other IP address?

Isn’t the solution you are looking for just: require ‘socket’ addr_infos = Socket.ip_address_list Since a machine can have multiple interfaces and multiple IP Addresses, this method returns an array of Addrinfo. You can fetch the exact IP addresses like this: addr_infos.each do |addr_info| puts addr_info.ip_address end You can further filter the list by rejecting loopback … Read more

HTML to Plain Text with Ruby?

Actually, this is much simpler: require ‘rubygems’ require ‘nokogiri’ puts Nokogiri::HTML(my_html).text You still have line break issues, though, so you’re going to have to figure out how you want to handle those yourself.

Rails console is not outputting SQL Statements to my Development Log

the rails console never writes to the log file, but you can achieve it quite easily, for example, if you execute following after starting the rails console ActiveRecord::Base.logger = Logger.new STDOUT rails will log all SQL statements to stdout, thus display them in your terminal. and since Logger.new accepts any stream as first argument, you … Read more

How to calculate the distance between two GPS coordinates without using Google Maps API?

Distance between two coordinates on earth is usually calculated using Haversine formula. This formula takes into consideration earth shape and radius. This is the code I use to calculate distance in meters. def distance(loc1, loc2) rad_per_deg = Math::PI/180 # PI / 180 rkm = 6371 # Earth radius in kilometers rm = rkm * 1000 … Read more

What is the argument against using before, let and subject in RSpec tests? [closed]

Interesting question; something that I want to know more about as well…. So dug in a bit, and here is what I uncovered: Thoughtbot style guide dictum about let, etc. In an earlier version of the style guide, there’s more to that statement: Avoid its, let, let!, specify, subject, and other DSLs. Prefer explicitness and … Read more