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 Array | |
| def my_map | |
| ary = [] | |
| self.each do |elem| | |
| ary << yield(elem) | |
| end | |
| ary | |
| 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
| def yield_with_return_value | |
| hello_world = yield | |
| puts hello_world | |
| end | |
| yield_with_return_value { "Hello World!" } # => Hello World! |
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_with_arguments | |
| hello = 'Hello' | |
| world = 'World!' | |
| yield(hello, world) | |
| end | |
| yield_with_arguments { |hello, world| puts "#{hello} #{world}" } # => Hello World! |
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 optional_block | |
| yield if block_given? | |
| end | |
| optional_block # => nil | |
| optional_block { puts 'optional block' } # => optional 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_without_block | |
| yield | |
| end | |
| yield_without_block # => LocalJumpError (no block given (yield)) |
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 one_yield | |
| yield | |
| end | |
| def multiple_yields | |
| yield | |
| yield | |
| end | |
| one_yield { puts "one yield" } |
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
| computer.cores = 2 |
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
| computer[:screens] = 2 |
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
| require 'ostruct' | |
| computer = OpenStruct.new(ram: '4GB') |
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
| computer.instance_variables # => [:@table, :@modifiable] |