Set time zone offset in Ruby

Set the TZ environment variable… $ ruby -e ‘puts’ Sat Jan 15 20:49:10 -0800 2011 $ TZ=UTC ruby -e ‘puts’ 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

Determine if a string is a valid float value

Here’s one way: class String def valid_float? # The double negation turns this into an actual boolean true – if you’re # okay with “truthy” values (like 0.0), you can remove it. !!Float(self) rescue false end end “a”.valid_float? #false “2.4”.valid_float? #true If you want to avoid the monkey-patch of String, you could always make this … 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 assets pipeline “Cannot allocate memory – nodejs”

It’s simple to spend the three minutes (maybe two if you type fast) to add a swap file to your server. If you’re running Ubuntu (not sure how well this works for other Linux flavors), just follow this tutorial from DigitalOcean: Voila!