Skip to content

Instantly share code, notes, and snippets.

# https://mixandgo.com/learn/mastering-ruby-blocks-in-less-than-5-minutes
def my_method
puts "reached the top"
yield("John", 2)
puts "reached the bottom"
end
my_method { puts "reached yield" }
@cpkenn09y
cpkenn09y / ruby_hashes_explained.rb
Last active May 30, 2018 17:51
Ruby Hashes Explained
<<-OLIVER_QUESTION_20180530
Oliver's Question: what does "validate" mean here?
self.save(validate: false)
I'm trying to understand what var: means (specifically, the colon symbol)
Is this specifying the name of the parameter that the value is to be matched up with?
OLIVER_QUESTION_20180530
# -- LESSON: In Ruby, a hash can use Strings (eg. "almond", "twix") or symbols (eg. :almond, :peanut) as keys. -- #
def pretty_format_from_seconds(total_seconds)
milliseconds = total_seconds % 1 * 1000
seconds = total_seconds % 60
minutes = (total_seconds / 60) % 60
hours = total_seconds / (60 * 60)
return format("%02d:%02d:%02d:%02d", hours, minutes, seconds, milliseconds) #=> "01:00:00"
end
# Use ActiveRecord to find records based on an array of ids
# works with postgresql
scope :where_by_ordered_ids, ->(ids) {
order = sanitize_sql_array(
["position((',' || id::text || ',') in ?)", ids.join(',') + ',']
)
where(:id => ids).order(order)
}
# spec/helpers/support/request_helpers.rb
# Add the following require statement to your rails_helper.rb
# require Rails.root.join("spec/support/request_helpers.rb")
module Requests
module JsonHelpers
def json
## IMPORTANT -> The expectation must come before the invocation, so that it can catch the invocation when it happens
# Able to stub an instance's method. Will return whatever is in the block. The instance's method would have to be invoked in the RSpec.
@person.stub(:get_relevant_experts) { @collection_of_possibilities }
# Expectation of an instance that is available in the testing context to receive a particular method, choose return value
expect(@person).to receive(:get_relevant_experts).with("Medical").and_return(@collection_of_possibilities)
# Expectation of an instance to receive a method and return a result
expect_any_instance_of(Classification).to receive(:valid_matches).and_return(@collection_of_possibilities)
@cpkenn09y
cpkenn09y / excel_file_opener.rb
Created January 6, 2016 00:59
Simple implementation of opening a excel file
@xlsx = Roo::Spreadsheet.open(params[:file])
@header = @xlsx.sheet(0).row(1) # ["name", "ssn"]
@body = []
@xlsx.sheet(0).each_with_index(:name => 'name', :ssn => 'ssn') do |hash, index|
if (index == 0)
else
@body << hash
end
end
@cpkenn09y
cpkenn09y / zoo.js
Last active December 26, 2015 02:09 — forked from dbc-challenges/zoo.js
<html>
<body>
<script>
//------------------------------------------------------------------------------------------------------------------
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here.
//------------------------------------------------------------------------------------------------------------------
function Animal(species, numOfLegs) {
this.name = species;
//------------------------------------------------------------------------------------------------------------------
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here.
//------------------------------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------
// DRIVER CODE: Do **NOT** change anything below this point. Your task is to implement code above to make this work.
//------------------------------------------------------------------------------------------------------------------
@cpkenn09y
cpkenn09y / bubble_sort.rb
Created September 22, 2013 01:02
Bubble Sorts!! Able to see it unfold in the terminal :)
def bubble(array)
incrementer = 0
until incrementer == array.length - 1
array.each_with_index do |e,i|
p array
unless array[i+1] == nil
if array[i] > array[i+1]
array[i] = array[i+1]
array[i+1] = e
end