Eventmachine gem install fail

I could install it, doing this steps: 1) tried a normal install: gem install eventmachine it fetched the version 1.0.3 of the gem, but failed in the make, because of a variable declaration conflit 2) edited the file: c:\Ruby200-x64\lib\ruby\gems\2.0.0\gems\eventmachine-1.0.3\ext\project.h and commented the line 97 //typedef int pid_t; for a more robust correction, checkout the solution … Read more

Iterate over Ruby Time object with delta

Prior to 1.9, you could use Range#step: (start_time..end_time).step(3600) do |hour| # … end However, this strategy is quite slow since it would call Time#succ 3600 times. Instead, as pointed out by dolzenko in his answer, a more efficient solution is to use a simple loop: hour = start_time while hour < end_time # … hour … Read more

All factors of a given number

In Ruby, the prime library gives you the factorization: require ‘prime’ 4800.prime_division #=> [[2, 6], [3, 1], [5, 2]] To get that list of yours, you take the cartesian product of the possible powers: require ‘prime’ def factors_of(number) primes, powers = number.prime_division.transpose exponents = powers.map{|i| (0..i).to_a} divisors = exponents.shift.product(*exponents).map do |powers| primes.zip(powers).map{|prime, power| prime ** … Read more

What are the magic $-prefixed variables in Ruby? [closed]

Their name is global variables. There are several different references. You can get a full list by calling the method Kernel#global_variables puts global_variables Ruby also includes a file called “English.rb” in the standard library which provides an in-depth explanation of several global variables. Also, there’s (an archived version of) “Cryptic Ruby Global Variables and Their … Read more

Why isn’t current directory on my Ruby path? [duplicate]

In Ruby 1.9.2 the Powers that Be introduced an explicit change so that the working directory is no longer in the Ruby path. I thought it was the Apocalypse and a terrible thing, until I learned about require_relative. My apps tend to look like this: require ‘some_gem’ require ‘another_gem’ require_relative ‘lib/init’ And then lib/init.rb can … Read more

unzip (zip, tar, tag.gz) files with ruby

To extract files from a .tar.gz file you can use the following methods from packages distributed with Ruby: require ‘rubygems/package’ require ‘zlib’ tar_extract = Gem::Package::TarReader.new(Zlib::GzipReader.open(‘Path/To/myfile.tar.gz’)) tar_extract.rewind # The extract has to be rewinded after every iteration tar_extract.each do |entry| puts entry.full_name puts entry.directory? puts entry.file? # puts entry.read end tar_extract.close Each entry of type Gem::Package::TarReader::Entry … Read more

Ruby local variable is undefined

In Ruby local variables only accessible in the scope that they are defined. Whenever you enter/leave a Class, a Module or a Method definiton your scope changes in Ruby. For instance : v1 = 1 class MyClass # SCOPE GATE: entering class v2 = 2 local_variables # => [“v2”] def my_method # SCOPE GATE: entering … Read more