Skip to content

Instantly share code, notes, and snippets.

View lkrych's full-sized avatar

Leland Krych lkrych

  • Cisco
  • San Francisco, CA
View GitHub Profile
@lkrych
lkrych / letterchanges.rb
Created August 15, 2016 14:49
LetterChanges
def LetterChanges(str)
lower = "abcdefghijklmnopqrstuvwxyz"
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
vowels = "aeiou"
newString = []
stringarray = str.chars
stringarray.each do |letter|
if letter.match(/^[[:alpha:]]$/)
index = lower.index(letter)
@lkrych
lkrych / factorial_spec.rb
Created October 11, 2016 18:29
RSpec for factorial function
require_relative '../factorial.rb'
describe "Test the factorial function" do
it 'returns the right answer for 5' do
expect(factorial(5)).to eq(120)
end
it 'returns the right answer for 10' do
expect(factorial(10)).to eq(3628800)
end
it 'returns an error message for -5' do
@lkrych
lkrych / factorial.rb
Created October 11, 2016 18:40
Factorial function in ruby
def factorial(n)
if n <= 0
puts "sorry I only compute the factorial for positive numbers"
return
else
@product = 1
(1..n).each do |number|
@product *= number
end
return @product
@lkrych
lkrych / factorial_while.rb
Created October 11, 2016 19:49
Rewrite of the factorial method, Being used in RSpec blog post
def factorial_while(n)
if n <= 0
puts "sorry I only compute the factorial for positive numbers"
return
else
idx = n
product = 1
while idx > 1
product *= idx
idx -= 1
@lkrych
lkrych / rotten_potatoes_setup.rb
Last active October 17, 2016 17:42
Getting the Repo Setup so that you can test the controller!
bundle install #installs all the gems you need to run the app
rake db:seed # seeds the database with movies, to see the movies you are seeding with navigate to rottenpotatoes/db/seeds.rb
rake db:test:prepare
rails s # Boot it up!
@lkrych
lkrych / movies_controller.rb
Last active October 16, 2016 15:36
CRUD functionality in the movies controller
class MoviesController < ApplicationController
#NOTE: you can ignore show and index, we are only going to test the CRUD functions.
def movie_params
params.require(:movie).permit(:title, :rating, :description, :release_date)
end
def show
id = params[:id] # retrieve movie ID from URI route
@lkrych
lkrych / rspec_rails_new
Last active October 16, 2016 15:51
Generate basic rspec rails functionality
rails generate rspec:install
@lkrych
lkrych / controller_generator.rb
Created October 17, 2016 16:18
RSpec controller spec generator
rails generate rspec:controller movies
@lkrych
lkrych / create_movies.rb
Created October 17, 2016 16:23
Create method pulled from movies_controller
class MoviesController < ApplicationController
#some code
def create
@movie = Movie.create!(movie_params)
flash[:notice] = "#{@movie.title} was successfully created."
redirect_to movies_path
end
@lkrych
lkrych / read_movies.rb
Created October 17, 2016 16:25
Read method pulled from movies_controller
class MoviesController < ApplicationController
#some code
def show
id = params[:id] # retrieve movie ID from URI route
@movie = Movie.find(id) # look up movie by unique ID
# will render app/views/movies/show.<extension> by default
end