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

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

How to use “RVM –default” on MacOSX

I had the same problem once. It turned out the rvm-script got loaded twice, which broke things a bit. Check all the files that load when you open a shell: /etc/profile ~/.bashrc ~/.bash_profile and so on, and make sure they don’t load RVM twice. Maybe put echo “Going to load RVM” before [[ -s “/Users/user/.rvm/scripts/rvm” … 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

Are strings mutable in Ruby?

Yes, << mutates the same object, and + creates a new one. Demonstration: irb(main):011:0> str = “hello” => “hello” irb(main):012:0> str.object_id => 22269036 irb(main):013:0> str << ” world” => “hello world” irb(main):014:0> str.object_id => 22269036 irb(main):015:0> str = str + ” world” => “hello world world” irb(main):016:0> str.object_id => 21462360 irb(main):017:0>

Using layouts in HAML files independently of Rails

You’re mixing up two distinct Rails feature: partials (using render) and layouts (using yield). You can add a rails-like version of either (or both) of them to a Haml only program. Partials In a rails view, you can use render :partial_name to cause the file _partial_name.html.haml to be rendered at that point in the containing … Read more

Running another ruby script from a ruby script

require “b.rb” will execute the contents of b.rb (you call leave off the “.rb”, and there is a search path). In your case, you would probably do something like: a.rb: require “b.rb”; b(“Hello”, “world”) b.rb: def b(first, second) puts first + “, ” + second end Note that if you use require, Ruby will only … Read more

Include jekyll / liquid template data in a YAML variable?

Today I ran into a similar problem. As a solution I created the following simple Jekyll filter-plugin which allows to expand nested liquid-templates in (e.g. liquid-variables in the YAML front matter): module Jekyll module LiquifyFilter def liquify(input) Liquid::Template.parse(input).render(@context) end end end Liquid::Template.register_filter(Jekyll::LiquifyFilter) Filters can be added to a Jekyll site by placing them in the … Read more