Created
May 22, 2012 17:25
-
-
Save chischaschos/2770412 to your computer and use it in GitHub Desktop.
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
| module B | |
| def greeting | |
| puts "Hi there" | |
| end | |
| end | |
| module C | |
| def yell | |
| puts "AAaaaaaaaaa!!" | |
| end | |
| end | |
| class A | |
| extend B | |
| include C | |
| end | |
| A.greeting | |
| A.new.yell | |
| class D; end | |
| D.extend B | |
| D.send :include, C | |
| D.greeting | |
| D.new.yell |
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 B | |
| def greeting | |
| puts "holaaaaa" | |
| end | |
| end | |
| class A < B | |
| end | |
| A.new.greeting | |
| # Re open A class | |
| class A | |
| def greeting | |
| puts "The new hoolaaaaaa" | |
| end | |
| end | |
| A.new.greeting |
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
| proc1 = lambda { puts "Created as a lambda" } | |
| proc2 = Proc.new do | |
| puts "Created as a proc" | |
| end | |
| proc1.call | |
| proc2.call | |
| def greeting a_proc | |
| a_proc.call | |
| end | |
| greeting proc1 | |
| greeting proc2 | |
| proc1 = lambda { |name| puts "Hi #{name}" } | |
| proc2 = Proc.new do |name| | |
| puts "Hi #{name}" | |
| end | |
| proc1.call "Juan" | |
| proc2.call "Pepe" | |
| def greeting a_proc, name | |
| a_proc.call name | |
| end | |
| greeting proc1, "Peca" | |
| def greeting name | |
| puts "Inside the greeting before yielding" | |
| yield name | |
| puts "Inside the greeting after yielding" | |
| end | |
| greeting "Maria" do |name| | |
| puts "Greeting #{name}" | |
| 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
| class Configuration | |
| attr_accessor :username, :password | |
| def initialize | |
| yield self | |
| end | |
| end | |
| c = Configuration.new do |config| | |
| config.username = 'juan' | |
| config.password = '123' | |
| end | |
| puts c.username | |
| puts c.password |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment