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
| when 'display' | |
| movies.each do |movie, rating| | |
| puts "#{movie}: #{rating}" | |
| 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
| 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 |
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
| # 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}" } |
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 my_first_proc | |
| Proc.new | |
| end | |
| proc = my_first_proc { "hello" } | |
| proc.call |
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
| # 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 |
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
| # 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!" } |
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 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}." } |
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
| # lambda | |
| lambda { |x| x.to_f * 0.0314 } | |
| # Proc | |
| Proc.new { |x| x.to_f * 0.0314 } |
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
| 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) |
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
| class Automobile | |
| # A mixture of automobile features go here. | |
| end |