| From | To | Expression |
|---|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Determines how many cpus the virtual machine should be given. Uses half of | |
| # what's available on the host with a default of 4. | |
| def numvcpus | |
| begin | |
| os_cpu_cores / 2 | |
| rescue | |
| 4 | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| gem search --no-vers | parallel -j0 -I$ 'curl -O `curl https://rubygems.org/api/v1/gems/$.json | jq -r .gem_uri`' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Weazel | |
| CHARS = [*'A'..'Z', ' '] | |
| def initialize target | |
| @target = target | |
| @best = Array.new(28) { CHARS.sample }.join | |
| end | |
| def call | |
| 0.upto Float::INFINITY do |n| |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Integer | |
| def collatz_chain_size | |
| n = self | |
| chain = [] | |
| until n == 1 | |
| chain << n | |
| n.even? ? n /= 2 : n = n * 3 + 1 | |
| end | |
| [chain.size, chain.first] | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyClass | |
| attr_accessor :user, :type | |
| def initialize user: 'smith', type: 'pleb' | |
| @user = user | |
| @type = type | |
| end | |
| end | |
| both = MyClass.new user: 'blah', type: 'pleb' | |
| just_one = MyClass.new type: 'pleb' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| str = "aaa [first_name] bar [first_name] [last_name]" | |
| last = str.scan(/\[last_name\]/).size | |
| first = str.scan(/\[first_name\]/).size | |
| remaining = str.gsub(/\[(first_name|last_name)\]/, '').size | |
| first * 15 + last * 10 + remaining |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'benchmark' | |
| hash = {} | |
| Benchmark.measure do | |
| 1_000_000.times do | |
| hash = {} | |
| end | |
| end.real | |
| #=> 0.245904 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # An octothorp (#) starts a single-line comment. | |
| =begin | |
| Starting with a =being and ending with a =end | |
| makes it a multi-line comment. | |
| =end | |
| ## | |
| # 1. Variables and flow control. | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fib n | |
| a, b = 1, 2 | |
| sum = 0 | |
| while a < n | |
| sum += a if a.even? | |
| a, b = b, a + b | |
| end | |
| sum | |
| end |