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
#!/usr/bin/env ruby | |
# Example of Recursion in Ruby: Simplifying a Fraction | |
########## non-recursive version: ######### | |
def findGcd (a, b) | |
while b != 0 do |
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:
You can send a Ruby hash to a JavaScript where it can be used as if declared as an object literal in JavaScript!
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 |
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.
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
rules https://gist.github.com/radar/996779
apt-get install irssi
Enter irssi, connect to freenode and setup authorization:
#!/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." |