Skip to content

Instantly share code, notes, and snippets.

View georgebonnr's full-sized avatar

George Bonner georgebonnr

View GitHub Profile
@georgebonnr
georgebonnr / ruby_errors.rb
Last active May 6, 2019 01:51
Ruby errors
# Ruby-doc documentation from http://ruby-doc.org/core-2.0/Object.html#method-i-class
guy = "Bob"
guy.define_singleton_method(:hello) { "#{self}: Hello there!" }
guy.hello #=> "Bob: Hello there!"
# ------------------------------------------------------------------------------
# Here is what I get on my machine:
guy = "Bob"
@georgebonnr
georgebonnr / ltouch.txt
Created June 13, 2013 19:16
Lazy Touch. A way to create and open a file in its default application in one bash command. For instance, $ ltouch example.rb Add the text below to your user .bash_profile.
ltouch()
{
touch $1
open $1
}
@georgebonnr
georgebonnr / dumbdogs.rb
Created June 12, 2013 07:41
Another learning exercise. Learning to metaprogram.
class Dog
def initialize(name)
@name = name
end
def method_missing(m, *args, &block)
puts "#{@name} doesn't know how to #{m} yet!"
end
def eigenclass
@georgebonnr
georgebonnr / reversopunctuator.rb
Created June 12, 2013 04:04
Learning exercise - original exercise was to build a program that would reverse the word order of a string. I extended it by preserving the location of the punctuation (minus apostrophes) in the sentences to make the reversed strings a little more readable / interesting. First attempt - a little sloppy, but it works!
puts "Please enter a sentence."
user_input = gets.chomp
array = user_input.downcase.split(' ')
punct_indices = {}
array.each do |x|
contains_punct = /[[:punct:]&&[^']]/ =~ x
if contains_punct
@georgebonnr
georgebonnr / name_guesser.rb
Created June 5, 2013 16:46
Rough outline of a program that, given a set of initials, iterates randomly through strings of letters until it recreates those initials. Will (unrealistically) accept any number of initials, but anything over 4 characters usually takes it too long to solve to be practical. Just a little personal experiment.
name_arr = []
name_str = ''
print "What are your initials? "
correct_name = gets.chomp.upcase
print "Thanks! Let's see how long it takes me to randomly re-create your initials."
sleep 3
until name_str == correct_name
letter_set = ('A'..'Z').to_a