Created
January 7, 2014 15:39
-
-
Save shikhalev/8301163 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
# encoding: utf-8 | |
class Alpha | |
attr_accessor :alpha | |
def beta | |
self.alpha = 1 | |
alpha = 2 | |
end | |
end | |
a = Alpha.new | |
a.beta | |
p a.alpha |
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
# encoding: utf-8 | |
# a = 'INIT' | |
def alpha | |
a ||= :a | |
p [:alpha, a] | |
end | |
define_singleton_method :beta do | |
a ||= :b | |
p [:beta, a] | |
end | |
a = 'TEST' | |
alpha | |
beta | |
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
# encoding: utf-8 | |
a = 'A' | |
b = 'B' | |
2.times do |a| | |
b = a | |
p [a, b] | |
end | |
p [a, b] |
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
# encoding: utf-8 | |
class Alpha | |
def alpha | |
end | |
end | |
a = Alpha.new | |
a.define_singleton_method :beta do | |
end | |
p a | |
p [a.class, a.class.instance_methods(false)] | |
p [a.singleton_class, | |
a.singleton_class.instance_methods(false)] | |
p a.class.ancestors | |
p a.singleton_class.ancestors | |
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
# encoding: utf-8 | |
module Alpha | |
@@alpha = 'A' | |
def alpha= x | |
@@alpha = x | |
end | |
def alpha | |
@@alpha | |
end | |
end | |
class Beta | |
include Alpha | |
end | |
class Gamma | |
include Alpha | |
end | |
b = Beta.new | |
g = Gamma.new | |
p b.alpha | |
b.alpha = 'B' | |
p g.alpha | |
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
# encoding: utf-8 | |
class Alpha | |
@@alpha = 'A' | |
def Alpha.alpha | |
@@alpha | |
end | |
def set_alpha x | |
@@alpha = x | |
end | |
end | |
a = Alpha.new | |
a.set_alpha 'X' | |
p Alpha.alpha | |
class Beta < Alpha | |
@@alpha = 'B' | |
end | |
p Alpha.alpha |
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
# encoding: utf-8 | |
class Alpha | |
ALPHA = 'A' | |
end | |
module Beta | |
ALPHA = 'B' | |
class Gamma < Alpha | |
def Gamma.alpha | |
ALPHA | |
end | |
end | |
end | |
p Beta::Gamma.alpha | |
p Beta::Gamma::ALPHA |
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
# encoding: utf-8 | |
class Alpha | |
attr_accessor :alpha | |
end | |
# alpha = 'a' | |
x = 'x' | |
a = Alpha.new | |
a.alpha = 'A' | |
a.instance_eval do | |
p [alpha, x, self] | |
self.alpha = x | |
end | |
alpha = 'a' | |
p [alpha, x, a] |
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
# encoding: utf-8 | |
class Alpha | |
end | |
Alpha.instance_eval do | |
def alpha | |
end | |
end | |
Alpha.module_eval do | |
def beta | |
end | |
end | |
p [Alpha.methods(false), | |
Alpha.instance_methods(false)] |
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
# encoding: utf-8 | |
class Alpha | |
def name_method name | |
define_singleton_method name do | |
"name: #{name}" | |
end | |
end | |
end | |
a = Alpha.new | |
a.name_method :alpha | |
a.name_method :beta | |
p [a.alpha, a.beta] |
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
# encoding: utf-8 | |
def get_binding | |
local = 100 | |
return binding | |
end | |
b = get_binding | |
b.eval 'p local' |
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
# encoding: utf-8 | |
a = :start | |
t = Thread.new do | |
sleep 1 | |
Thread.exclusive { p a } | |
end | |
a = :continue | |
t.join |
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
# encoding: utf-8 | |
$a = :start | |
fork do | |
sleep 1 | |
p $a | |
end | |
$a = :continue | |
Process.wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment