XPath axis, get all following nodes until

Use: (//h2[. = ‘Foo bar’])[1]/following-sibling::p [1 = count(preceding-sibling::h2[1] | (//h2[. = ‘Foo bar’])[1])] In case it is guaranteed that every h2 has a distinct value, this may be simplified to: //h2[. = ‘Foo bar’]/following-sibling::p [1 = count(preceding-sibling::h2[1] | ../h2[. = ‘Foo bar’])] This means: Select all p elements that are following siblings of the h2 … Read more

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