Created
June 27, 2012 20:58
-
-
Save pzaich/3006810 to your computer and use it in GitHub Desktop.
Passing optional hash arguments
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 an optional hash | |
| # | |
| # **branch based off of the optional hash | |
| def initialize(task_string, created_at = Time.now, completed_at = nil) | |
| end | |
| def initialize(task_string, optional_values = {}) | |
| values = {created_at: Time.now, completed_at: nil}.merge(optional_values) | |
| @task = task_string | |
| @created_at = values[:created_at] | |
| @completed_at = values[:completed_at] | |
| end | |
| class WordProcessor | |
| def initialize (string, conditional_arguments = {}) | |
| @string = string | |
| if conditional_arguments[:reverse] | |
| reverse_the_string! | |
| elsif conditional_arguments[:capitalize] | |
| capitalize_the_string! | |
| elsif conditional_arguments[:insert] | |
| insert_spaces_into_string! | |
| end | |
| end | |
| def print_string | |
| puts @string | |
| end | |
| def reverse_the_string! | |
| @string = @string.split('').reverse.join('') | |
| end | |
| def capitalize_the_string! | |
| @string.upcase! | |
| end | |
| def insert_spaces_into_string! | |
| @string = @string.split('').join(' ') | |
| end | |
| end | |
| new_foobar = WordProcessor.new("This is a string") | |
| new_foobar.print_string | |
| new_foobar = WordProcessor.new("This is a string", reverse: true) | |
| new_foobar.print_string | |
| new_foobar = WordProcessor.new("This is a string", capitalize: true) | |
| new_foobar.print_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment