Skip to content

Instantly share code, notes, and snippets.

when 'display'
movies.each do |movie, rating|
puts "#{movie}: #{rating}"
end
when 'delete'
puts "What movie do you want to delete?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "Movie not found!"
else
movies.delete(title.to_sym)
puts "#{title} has been removed."
end
else
@sirramongabriel
sirramongabriel / proc_lambda1.rb
Last active December 22, 2015 18:29
Demonstrates how to create a Proc object
# creating a proc inside a method
def my_first_proc
Proc.new
end
# creating a proc assigned to a variable
my_second_proc = Proc.new { |val| puts "#{val}" }
@sirramongabriel
sirramongabriel / proc_lambda2.rb
Created September 10, 2013 18:10
Demonstrates the 'call' method being used on the variable housing the code block.
def my_first_proc
Proc.new
end
proc = my_first_proc { "hello" }
proc.call
@sirramongabriel
sirramongabriel / proc_lambda3.rb
Created September 10, 2013 18:21
Demonstrates how to use the collect method and a Proc.
# show only even numbers in an array
numbers = [1,2,3,4,5,6,7,8,9,10,11]
doubled_fibs = fibs.collect { |fib_num| fib_num * 2 }
even_numbers = numbers.collect do |num|
if num % 2 == 0
puts num
end
@sirramongabriel
sirramongabriel / proc_lamdba4.rb
Created September 10, 2013 18:26
Demonstrates yield within a method allowing the method to accept a code block.
# allow our method to accept a block
def block_test
puts "I'm in the method!"
puts "Yielding to the block..."
yield
puts "I'm back in the method!"
end
block_test { puts "--- And splish splash... I'm the block!" }
@sirramongabriel
sirramongabriel / proc_lambda5.rb
Created September 10, 2013 18:31
Demonstrates parameter passing with yield.
def yield_name(name)
puts "I'm in the method!"
yield name
puts "Block complete! Back in the method."
end
yield_name("Ramon") { |name| puts "My first name is #{name}." }
@sirramongabriel
sirramongabriel / lambda1.rb
Created September 11, 2013 18:05
Demonstrates the setup of a lambda and then a Proc
# lambda
lambda { |x| x.to_f * 0.0314 }
# Proc
Proc.new { |x| x.to_f * 0.0314 }
@sirramongabriel
sirramongabriel / lambda2.rb
Created September 11, 2013 18:41
Demonstrates a lambda in various conditions as pertains to parameter passing.
lam - lambda { |x| p x }
# => <Proc:0n79er3l@(irb) :7 (lambda)>
# call with the 'x' paramerter
lam.call(1)
# => 1
# call without the 'x' parameter
lam.call
# => ArgumentError: wrong number of arguments (0 for 1)
@sirramongabriel
sirramongabriel / classes1.rb
Created September 15, 2013 23:13
Demonstrates building a class from scratch
class Automobile
# A mixture of automobile features go here.
end