Skip to content

Instantly share code, notes, and snippets.

def big(arr)
re = arr.collect{|x| x*100}
puts re
end
big([1,2,3,4])
f = Proc.new
f = Proc.new{|x| x+100}
f
def what(x)
f(x)
make a file ~/.pryrc
Pry.config.commands.command "history", "Get pry input history" do
Readline::HISTORY.to_a.map{|x| puts x}
end
Now... in pry typing "history" will print your history in a list
@allthetime
allthetime / README.md
Last active August 29, 2015 14:06 — forked from kvirani/README.md

Create a directory in your /vagrant folder (while ssh'd into your Vagrant VM). Within that directory, clone your fork of this repo, which contains one ruby file max.rb.

Spend the time necessary to read and fully understand what the code in max.rb is doing. Google or discuss as necessary. Have an expectation of what will be output when you first run the code, then use the ruby command to run the max.rb file from the terminal.

Task: Currently, the built-in Array#max method is being used (line 3) to implement the logic for the maximum method. As an exercise, instead of leveraging this built-in method, implement your own logic such that the maximum method continues to work the same way that it was, and the resulting output from this ruby script stays the same. Note: you also cannot use Ruby's built-in sort method.

@allthetime
allthetime / README.md
Last active August 29, 2015 14:06 — forked from kvirani/README.md

You should be familiar with this code, but go ahead and execute it to make sure it’s working as expected.

Let’s now make our fizzbuzz more robust and flexible.

Task 1

Refactor (improve) this code by implementing a method (called fizzbuzz) that takes in the starting and ending number so that we can programmatically change which numbers our fizzbuzz starts and ends at, instead of the usual 1 and 100.

Task 2

Think about other methods that can be implemented to further refactor this code and make it more readable. Remember that readability does not necessarily mean fewer lines of code.

@allthetime
allthetime / README.md
Last active August 29, 2015 14:06 — forked from kvirani/README.md

The Yuppie Vancouverite

The Yuppie Vancouverite needs to rent an apartment downtown!

Here we have a rent? method that helps them decide if they are going to an apartment based on some information about it. It's passed 3 key pieces of information that are used to determine this:

  1. Is the apartment furnished? Since this is a yes/no thing, it's a boolean (true/false)
  2. Is the apartment baller? Since this is a yes/no thing, it's a boolean (true/false)
  3. The monthly rent for the apartment. This is expected to be an integer (Fixnum)
@allthetime
allthetime / README.md
Last active August 29, 2015 14:06 — forked from kvirani/README.md

Your cofounder has left for Hawaii and asked that you take care of his puppy for 2 weeks.

As it turns out, this dog is a menace and won’t stop barking. His name is Shakil. He exhibits the following inexplicable behavior:

If you say anything to him, he will just bark back once ("woof"), except:

  • If you pretend to be a dog and bark ("woof") at him, he will go a bit nuts and woof back at you three times, pretty loudly: "WOOF WOOF WOOF"
  • If you explicitly use his name and tell him to stop (either "shakil stop" or "Shakil STOP!") he will not respond back with a bark (enjoy your moment of peace)
  • If you pretend to be a cat and "meow" at him, he will go berserk and woof back at you five times: "woof woof woof woof woof"
  • If you say anything else but with the word "treat" thrown into the mix, he won’t bark back, thinking he’ll be getting a treat
  • If you say "go away" he manages to actually leave you alone by leaving the room.
@allthetime
allthetime / README.md
Last active August 29, 2015 14:06 — forked from kvirani/README.md

Create a directory in your /vagrant folder (while ssh'd into your Vagrant VM). Within that directory, clone your fork of this repo, which contains one ruby file sort.rb.

Spend the time necessary to read and fully understand what the code in sort.rb is doing. Google or discuss as necessary. Have an expectation of what will be output when you first run the code, then use the ruby command to run the sort.rb file from the terminal.

Task: Currently, the built-in Array#sort method is being used (line 3) to implement the logic for the sort method. As an exercise, instead of leveraging this built-in method, implement your own sort logic such that the resulting output from this ruby script stays the same. See http://en.wikipedia.org/wiki/Sorting_algorithm for different sorting algorithms. At a minimum implement Bubble Sort and Merge Sort.

As an extension, benchmark the different implementations versus the built in

@allthetime
allthetime / gist:dbb8ddddc345d20d4cda
Created September 3, 2014 21:24
ruby bubble sort
def bubble(arr)
n = arr.length-1
loop do
swapped = false
(1..n).each do |i|
if arr[i-1] > arr[i] then
remember = arr[i]
arr[i] = arr[i-1]
arr[i-1] = remember
swapped = true
=begin
$15 per sq ft
< 2 colors = $10/color else $15/color
tax = 15%
=end
@allthetime
allthetime / README.md
Last active August 29, 2015 14:06 — forked from kvirani/README.md

Introduction

One of the first questions a good programmer will ask themselves when they encounter a bug is "what is the error message telling me?". Error messages are your friend, not your enemy.

Stack Traces

Before you start, take a look at and run error_example.rb. The output from running the program is this:

error_example.rb:9:in `method3': wrong number of arguments (0 for 1) (ArgumentError)

from error_example.rb:6:in `method2'