Last active
December 27, 2015 05:39
-
-
Save psyomn/7275528 to your computer and use it in GitHub Desktop.
Evil Eval
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 Person | |
| end | |
| person = Person.new | |
| Person.instance_eval "attr_accessor :name" | |
| person.name = "bobby" | |
| puts person.name | |
| person.instance_eval "def greet; puts 'hello ' end" | |
| person.greet | |
| person2 = Person.new | |
| begin | |
| person2.greet | |
| rescue | |
| puts "Yep only object person has this behavior" | |
| end | |
| puts "Type in a method: " | |
| person.instance_eval $stdin.gets.chomp | |
| p person.methods - Object.instance_methods | |
| # Output: | |
| # bobby | |
| # hello | |
| # Yep only object person has this behavior | |
| # Type in a method: def methy; 'hello' end | |
| # [:greet, :methy, :name, :name=] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment