Skip to content

Instantly share code, notes, and snippets.

View hemju's full-sized avatar
😃
Work Work

Helmut M Juskewycz hemju

😃
Work Work
View GitHub Profile
# Taken from http://benoithamelin.tumblr.com/ruby1line/ All credits go to Benoit Hamelin
# Ruby one-liners
# This is a translation of Eric Pement’s collection of Awk one-liners as Ruby one-liners. These so-called one-liners are small programs that hold on a single (sometimes longish) line of code, so it may be run from the command line, typically for text processing purposes. So, all problems solved by Awk one-liners on the page linked above are solved here in Ruby, sorted along the same categories as Pement’s work. In some cases, multiple solutions are proposed, as they outline nice features or idiosyncrasies of the Ruby language and conventions.
# Note that this is not the first collection of Ruby one-liners: googling “Ruby one-liner” yields multiple hits. However, I have put up this collection by myself, without looking at other solutions, for the sake of practice. I have posted about what generalities I have learned throughout this exercise here.
# File spacing
# Double-space a file.
ruby -ne 'print; pu
@hemju
hemju / connect-heroku-app-to-postgres-rds-with-ssl.md
Created June 21, 2017 15:03 — forked from glarrain/connect-heroku-app-to-postgres-rds-with-ssl.md
How to connect a Heroku application to an Amazon RDS PostgreSQL instance, forcing SSL and certificate chain verification

1 - Download the RDS certificates (root plus region-specific intermediate ones) bundle:

wget -O config/rds-combined-ca-bundle.pem https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem

2 - Add config/rds-combined-ca-bundle.pem to the repository and redeploy to Heroku.

3 - Update the DATABASE_URL env var:

@hemju
hemju / gist:94713cacdbac9d08071d
Last active August 29, 2015 14:03
Blog: Dynamic Ruby Example Example
def import_file_data(file_path, content, import_type, strategy_name, additional_attributes = {}, options = {})
# probably file_path is a String and the result file_name as well
file_name = File.basename(file_path)
# no idea what tempfile is, a string?
tempfile = write_to_temp_file(content, file_name)
uploaded_file = OpenStruct.new(:original_filename => file_name, :tempfile => tempfile)
# Is this now an instance of ResourceImport or a result of a resource import?
resource_import = ResourceImport.import!(user, project, uploaded_file, file_path, import_type, strategy_name, additional_attributes)
@hemju
hemju / PostgreSQL Index Check
Created June 3, 2014 13:48
Prints out all indices together with stats about size, reads, ....
SELECT
t.tablename,
indexname,
c.reltuples AS num_rows,
pg_size_pretty(pg_relation_size(quote_ident(t.tablename)::text)) AS table_size,
pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size,
CASE WHEN indisunique THEN 'Y'
ELSE 'N'
END AS UNIQUE,
idx_scan AS number_of_scans,
#!/usr/bin/env ruby
require 'bundler'
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]
@hemju
hemju / word_char_count
Created July 24, 2013 11:01
Shows two queries to get the word count and character count of a string/text column in Postgres.
# Character count
select sum(length(YOUR_COLUMN)) from YOUR_TABLE;
# Word count
select sum(array_length(regexp_split_to_array(YOUR_COLUMN, '\s'),1)) from YOUR_TABLE;
@hemju
hemju / stats.txt
Created May 9, 2012 09:39
rake stats of lingohub
+----------------------+-------+-------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers | 1729 | 1320 | 41 | 166 | 4 | 5 |
| Helpers | 604 | 442 | 1 | 72 | 72 | 4 |
| Models | 2133 | 1254 | 33 | 174 | 5 | 5 |
| Libraries | 4272 | 3101 | 100 | 373 | 3 | 6 |
| Model specs | 1988 | 1232 | 0 | 10 | 0 | 121 |
| View specs | 0 | 0 | 0 | 0 | 0 | 0 |
| Controller specs | 2656 | 2021 | 0 | 22 | 0 | 89 |
@hemju
hemju / gist:2500433
Created April 26, 2012 15:37
three ways to use HTML in Rails translations
1: <%= t 'html.escaped' %> <!-- INCORRECT RESULT: &lt;h2&gt;lingohub&lt;/h2&gt;&lt;p&gt;can't wait to see it&lt;/p2&gt; -->
2: <%= raw t('html.raw') %> <!-- CORRECT RESULT: <h2>lingohub</h2><p>can't wait to see it</p> -->
3: <%= t('html.html_safe').html_safe %> <!-- CORRECT RESULT: <h2>lingohub</h2><p>can't wait to see it</p> -->
4: <%= t 'html.ending_html' %> <!-- CORRECT RESULT: <h2>lingohub</h2><p>can't wait to see it</p> -->
@hemju
hemju / gist:2472073
Created April 23, 2012 16:25
translations
en:
html:
escaped: "<h2>lingohub</h2><p>can't wait to see it</p>"
raw: "<h2>lingohub</h2><p>can't wait to see it</p>"
html_safe: "<h2>lingohub</h2><p>can't wait to see it</p>"
ending_html: "<h2>lingohub</h2><p>can't wait to see it</p>"
@hemju
hemju / optimize_images.rb
Created February 9, 2012 12:32 — forked from meleyal/optimize_images.rb
Simple png + jpg optimization script using optipng + jpegtran
#!/usr/bin/ruby
# Instructions
# - Install optipng + libjpeg (http://mxcl.github.com/homebrew/)
# - Put this file in a directory of images
# - Open in TextMate + run it (cmd+r)
# Config