Skip to content

Instantly share code, notes, and snippets.

@slawosz
Created December 3, 2013 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slawosz/7774175 to your computer and use it in GitHub Desktop.
Save slawosz/7774175 to your computer and use it in GitHub Desktop.
# 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
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