Skip to content

Instantly share code, notes, and snippets.

function make_adder( left_operand ) {
return function ( right_operand ) {
return left_operand + right_operand;
};
}
// so, you can say:
var plus_two = make_adder(2);
var plus_three = make_adder(3);
alert(plus_two(5)); //=> 7
void str_remove_char ( char array[], int c ) {
int i, j;
for ( i = j = 0; array[i]; ++i ) {
if ( array[i] != c )
array[j++] = array[i];
}
array[j] = '\0';
}
#include <stdio.h>
void str_squeeze(char *s) {
int i, j;
char last = '\0';
for ( i = j = 0; s[i]; last = s[i++] )
if ( s[i] != last )
s[j++] = s[i];
s[j] = '\0';
}
@JosephPecoraro
JosephPecoraro / shell-execution.rb
Last active September 10, 2023 10:12
Shell Execution in Ruby
# Ways to execute a shell script in Ruby
# Example Script - Joseph Pecoraro
cmd = "echo 'hi'" # Sample string that can be used
# 1. Kernel#` - commonly called backticks - `cmd`
# This is like many other languages, including bash, PHP, and Perl
# Synchronous (blocking)
# Returns the output of the shell command
# Docs: http://ruby-doc.org/core/classes/Kernel.html#M001111
@JosephPecoraro
JosephPecoraro / gist:4397
Created August 7, 2008 13:18
Benchmarking String Concatenation
// Benchmarking String Concatenation Comparing
// 1. Array.join
// 2. String concatenation with '+'
// Test Functions
function test_join(content) { return ['output.push(', content, ');'].join(''); };
function test_str_concat(content) { return 'output.push(' + content + ');'; };
// Test Strings
var SHORT_STRING = 'test';
@JosephPecoraro
JosephPecoraro / gist:5049
Created August 12, 2008 14:42
Parse DOM Data
//
// Function: allData(node)
// Concatenate all the text data of a node's children.
//
function allData(node) {
var data = "";
if (node && node.firstChild) {
node = node.firstChild;
if (node.data) { data += node.data; }
while (node = node.nextSibling) {
@JosephPecoraro
JosephPecoraro / gist:6563
Created August 21, 2008 14:02
Strip similarities at the start and end of two strings. [ruby]
# Strip Similarities between two strings
module Kernel
#
# strip_similiar
# Strips the similiarities at the start and end of each
# of the given strings, returning a list of both of the
# stripped strings.
#
int str_cmp( char *str1, char *str2 ) {
if ( !*str1 && !*str2 )
return 0;
char a = *str1, b = *str2;
if( a >= 'a' && a <= 'z' )
a -= 32;
if( b >= 'a' && b <= 'z' )
b -= 32;
# DATA is a global that is actually a
# File object containing the data
# after __END__ in the current
# script file. WHOA!
puts DATA.read
__END__
I can put anything I want
after the __END__ symbol
and access it with the
require 'rubygems'
require 'hpricot' # hpricot-0.6.164
html = <<END
<html><head></head><body><table>
<tr><td>R0C0</td><td>R0C1</td><td>R0C2</td><td>R0C3</td></tr>
<tr><td>R1C0</td><td>R1C1</td><td>R1C2</td><td>R1C3</td></tr>
<tr><td>R2C0</td><td>R2C1</td><td>R2C2</td><td>R2C3</td></tr>
</table></body></html>
END