Deep merge hashes. If both values are numeric they are summed.
$ ruby deep_sum_merge.rb
source:
layer_1: 10
deeper:
layer_2: 20
deeper:
layer_3: 30
deeper:
#!/bin/sh | |
# WARNING: REQUIRES /bin/sh | |
# | |
# - must run on /bin/sh on solaris 9 | |
# - must run on /bin/sh on AIX 6.x | |
# | |
# Copyright:: Copyright (c) 2010-2015 Chef Software, Inc. | |
# License:: Apache License, Version 2.0 | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); |
Deep merge hashes. If both values are numeric they are summed.
$ ruby deep_sum_merge.rb
source:
layer_1: 10
deeper:
layer_2: 20
deeper:
layer_3: 30
deeper:
rbenv versions | |
which bundle | |
bundle --help |
Just messing around with parsing a log file with Ruby...
$ ruby apache_fun.rb
01:02:03 report
2 INFO messages
1 WARN messages
01:02:05 report
2 ERROR messages
1 INFO messages
# this is totally untested... | |
require 'rest-client' | |
require 'json' | |
PAGE_REGEX = /page=(?<page>\d+)/ | |
def get_paginated(base_url, length=30) | |
base_url += "?per_page=#{length}" | |
items = [] |
[chefvm stage_dr] simple-passenger-cookbook $ cat Berksfile | |
source 'https://supermarket.chef.io' | |
metadata | |
cookbook 'apt', '~> 4.0' | |
cookbook 'build-essential', '= 2.4.0' | |
[chefvm stage_dr] simple-passenger-cookbook $ rm Berksfile.lock | |
[chefvm stage_dr] simple-passenger-cookbook $ bundle exec berks install | |
Resolving cookbook dependencies... |
irb(main):001:0> def equals(a, b='B', c='C'); p a; p b; p c; end | |
=> :equals | |
irb(main):002:0> def colon(a, b: 'B', c: 'C'); p a; p b; p c; end | |
=> :colon | |
irb(main):003:0> equals('A', c='see') | |
"A" | |
"see" | |
"C" | |
=> "C" | |
irb(main):004:0> colon('A', c: 'see') |
class Hash | |
def to_java_properties_hash(prefix='') | |
properties = {} | |
self.each do |property, value| | |
new_prefix = prefix.empty? ? property.to_s : prefix + '.' + property.to_s | |
if value.respond_to? :to_java_properties_hash | |
properties.merge!(value.to_java_properties_hash(new_prefix)) | |
else | |
properties[new_prefix] = value.to_s |
require 'net/http' | |
require 'rspec' | |
class ApiClient | |
def initialize(server_url, username, password) | |
@server_url, @username, = server_url, username | |
authenticate(password) | |
end | |
def authenticate(password) |
output = 'peer count: 6 | |
ip address: 1.2.3.4 | |
status: up | |
client version: 1.2.3 | |
ip address: 1.2.3.5 | |
status: up | |
client version: 1.3.0 |