Last active
October 21, 2017 04:50
-
-
Save suhanlee/b342a1dc98fb667e6a6b21a807d9ad00 to your computer and use it in GitHub Desktop.
bindig example
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 Demo | |
def initialize(n) | |
@secret = n | |
end | |
def get_binding | |
return binding() | |
end | |
end | |
k1 = Demo.new(99) | |
b1 = k1.get_binding | |
k2 = Demo.new(-3) | |
b2 = k2.get_binding | |
eval("@secret", b1) | |
eval("@secret", b2) | |
eval("@secret") |
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 'erb' | |
class ErbTest | |
def initialize | |
@last_name = 'lee' | |
@first_name = 'kyle' | |
@erb = ERB.new(File.read('erb_template.erb')) | |
end | |
attr_accessor :last_name, :first_name | |
def to_s | |
@erb.result(binding) | |
end | |
def full_name(space) | |
first_name + space + last_name | |
end | |
end | |
erb_test = ErbTest.new | |
puts erb_test.to_s |
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 'erb' | |
class ErbTest | |
def initialize | |
@last_name = 'lee' | |
@first_name = 'kyle' | |
end | |
attr_accessor :last_name, :first_name | |
extend ERB::DefMethod | |
def_erb_method('to_s', 'erb_template.erb') | |
def full_name(space) | |
first_name + space + last_name | |
end | |
end | |
erb_test = ErbTest.new | |
puts erb_test.to_s |
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
Hi <%= last_name %> | |
<%= first_name %> | |
<%= full_name(" ") %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment