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
| #ruby bits - solutions - procs | |
| class Game | |
| attr_accessor :name, :year, :system | |
| def initialize(name, options = {}) | |
| @name = name | |
| @year = options[:year] | |
| @system = options[:system] | |
| 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
| # refactor this Proc def to use lambda | |
| library = Library.new(GAMES) | |
| print_details = Proc.new do |game| | |
| puts "#{game.name} (#{game.system}) - #{game.year}" | |
| end | |
| library.exec_game('Contra', print_details) | |
| # lambda | |
| library = Library.new(GAMES) |
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
| # Refactor exec_game to take a second Proc argument for error handling purposes. | |
| # If calling the action Proc raises an exeption, call the new error Proc. | |
| # We've already modified the code in main.rb to create the error Proc and pass it to exec_game. | |
| # Hint: You'll want to add a begin/rescue/end block around action.call. | |
| class Game | |
| attr_accessor :name, :year, :system | |
| def initialize(name, options = {}) | |
| @name = name | |
| @year = options[:year] |
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
| #ruby bits 2- solution 4 - proc to block | |
| # Move the code in the each block into a Proc, and then pass that Proc into library.each. | |
| class Game | |
| attr_accessor :name, :year, :system | |
| def initialize(name, options = {}) | |
| @name = name | |
| @year = options[:year] | |
| @system = options[:system] | |
| 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
| # using super | |
| class Parent | |
| def initialize(name) | |
| puts 'parent_' + name | |
| end | |
| end | |
| class Child < Parent |
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
| #ruby bits 2- solution 5 - capturing block | |
| # Refactor this each method to capture its block as a Proc object, then inside of the games.each | |
| # block, call the captured Proc instead of yielding. | |
| # OLD | |
| # class Library | |
| # attr_accessor :games | |
| # def initialize(games) | |
| # @games = games |
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
| # Refactor the each method to be just one line of code, passing the captured block directly to games.each. | |
| # OLD | |
| # class Library | |
| # attr_accessor :games | |
| # def initialize(games) | |
| # @games = games | |
| # 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
| # symbol to proc | |
| # Refactor the games.map call, removing its block and using Symbol#to_proc instead. | |
| # OLD | |
| # class Library | |
| # attr_accessor :games | |
| # def initialize(games) | |
| # @games = games | |
| # 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
| # Optional Blocks | |
| # Implement this list method that takes an optional block. If a block is passed, yield each game to the block | |
| # and print the result. If there is no block, print each game's name. | |
| # OLD | |
| # class Library | |
| # attr_accessor :games | |
| # def initialize(games) | |
| # @games = games |
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
| # short circuit evaluation - || | |
| def sign_in | |
| current_session || sign_user_in | |
| end |