This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<<-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. -- # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<body> | |
<script> | |
//------------------------------------------------------------------------------------------------------------------ | |
// YOUR CODE: Create your Zoo "object literal" and Animal "constructor" and "prototypes" here. | |
//------------------------------------------------------------------------------------------------------------------ | |
function Animal(species, numOfLegs) { | |
this.name = species; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------------------------------------------ | |
// 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. | |
//------------------------------------------------------------------------------------------------------------------ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |