Created
December 3, 2013 17:59
-
-
Save slawosz/7774175 to your computer and use it in GitHub Desktop.
This file contains 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
# global obj instance (instance of Object class) | |
puts self | |
# instance of class Class, so we can have objects of this class | |
class Klass | |
@foo = 'bar' | |
puts self #(Klass itself) | |
# we create instance method | |
# or (more precisly) method in current self | |
def foo | |
@foo = 'baz' | |
puts self #(instance of Klass class) | |
end | |
# we create class method | |
# or (more precisly) method in obj class | |
def self.foo | |
puts 'bla' | |
end | |
def self.has_many | |
puts 'bla' | |
end | |
has_many | |
end | |
Klass.new.foo |
This file contains 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 Obj | |
# $glob | |
# @@class_hier | |
# @class_instance_variable | |
# @inst_variable | |
def set_setter(arg) | |
@foo = arg | |
end | |
def setter=(arg) | |
@foo = arg | |
end | |
def print_foo | |
puts @foo | |
end | |
def <=>(other) | |
end | |
#tricky: | |
def tricky | |
# proper | |
# self.setter = 'blafoo' | |
# here ruby create local var, not use setter= method | |
setter = 'blafoo' | |
end | |
def tricky2 | |
# since there is no print_foo local var, ruby uses self here | |
print_foo | |
end | |
def block_with_yield(a) # def block(a, func) | |
yield a # func(a) | |
end | |
def block_with_lambda(a, &blk) # def block(a, func) | |
blk.call(a) # func(a) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment