Skip to content

Instantly share code, notes, and snippets.

View Jeff-Russ's full-sized avatar

Jeffrey Russ Jeff-Russ

View GitHub Profile

Run (almost) Any Language From a Bash Script

You probably know that when you make a bash script you give it a shebang like one of these:

#!/bin/sh
#!/bin/bash
#!/usr/local/bin/bash 
#!/usr/bin/env bash
@Jeff-Russ
Jeff-Russ / recursive_reduce_fraction.rb
Last active June 1, 2016 19:48
Example of Recursion in Ruby: Simplifying a Fraction
#!/usr/bin/env ruby
# Example of Recursion in Ruby: Simplifying a Fraction
########## non-recursive version: #########
def findGcd (a, b)
while b != 0 do
@Jeff-Russ
Jeff-Russ / Node.js: module.exports - Another Way .md
Last active June 13, 2016 21:14
Node.js: module.exports; Another Way

Node.js: module.exports - Another Way

You may want to make some files to require in others without saving the require to a variable like

var tools = require('./tools');

tools.some_method();
tools.some_other_method();

JS Objects vs Ruby Hashes

Ruby has iterators baked right in for all of it's containers:

whatever.each do |variable|
  # code
end

You can do this with Ruby arrays, hashes, objects, etc but in Javascript you don't have this most of the time. You have it for arrays but when you want something like an associative array your go-to is a Javascript object literal you pretend is an associate array by just not putting methods in:

Send a Ruby Hash to Node.js

You can send a Ruby hash to a JavaScript where it can be used as if declared as an object literal in JavaScript!

The Ruby Side: Explanation

You must first make sure that when any hash value is a string we have double-quotes that can survive the transmission process. How do you do that? By prepending and appending escaped double-quotes. Then you can create a json string with the entire hash.

def hash_each_pair_recursive(h, operation)
h.each_pair do |k,v|
if v.is_a?(Hash)
hash_each_pair_recursive(v, operation) # recurse
else # not recursing
operation.call k, v
end
end
end
@Jeff-Russ
Jeff-Russ / node-readline-sync.md
Last active May 14, 2019 11:55
A synchronous & blocking readline prompt solution for Node.js that's not a huge node package

node-readline-sync

a synchronous & blocking readline prompt solution for Node.js that's not a huge node package

Why ask the user for input if you're just going to go ahead and continue executing anyway? Usually when you prompt the user for a response, everything that transpires depends on the answer they give. When you ask the user "are you sure" before changing their legal name to "Diarrhea Jones", blocking is a GOOD THING.

Luckily there is a workaround that's so simple it doesn't actually need it's own git repository.

Installation Instructions


Ruby TDD with Minitest


Start with Minitest

Test::Unit is an implementation of the xUnit testing framework and was the recommended way to get started with TDD in Ruby on Rails since it is part of the Ruby standard library. Code that once was written with Test::Unit is now written in Minitest which ships with Ruby. Test::Unit is now deprecated and is now only part of Ruby for legacy support. Minitest is capable of both unit test and specification-flavored testing, as well as benchmarking. You can also install or upgrade the Minitest Gem this way:

$ gem install minitest --version 5.4.2

The other, heavier, option would be to go straight for the RSpec framework. This framework can be considered a domain-specific language (DSL) and resembles a natural language specification. Minitest, on the other hand, is Ruby. Although __RSp

#!/usr/bin/env bash
# BAShIC - BASIC interpreter written in Bash
# Based on bournebasic by Brandon S. Allbery 1987
#
# 10 print "Enter any number and I will double it for you ";
# 20 input x
# 30 y = 2*x
# 40 print "The answer is ";
# 50 print y
# 60 print "Enter -1 if you want to continue."