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
def call_block(&block) # NOTE: block is actually an instance of Proc | |
block.call 1, 2 | |
end | |
call_block { |a,b| puts "Hello #{a} and #{b}" } # "Hello 1 and 2" | |
def yield_params | |
yield 1, 2 | |
end | |
yield_params { |a,b| puts "#{a} and #{b}" } # "1 and 2" |
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 MyClass | |
def self.class_method # creates a class method. Always public | |
"class_method" | |
end | |
def method1 # default is 'public' | |
"method1" | |
end | |
protected # subsequent methods will be 'protected' | |
def method2 # will be 'protected' | |
"method2" |
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 MyClass | |
# define method 1 through 4 without and encapulation directives ... | |
public :method1, :method4 | |
protected :method2 | |
private :method3 | |
end | |
a = MyClass.new |
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 Person | |
attr :age | |
attr_reader :age | |
# gets translated into: | |
def age | |
@age | |
end | |
attr_writer :age | |
# gets translated into: |
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 Base | |
def initialize() | |
@x = 10 | |
end | |
end | |
d = Base.new | |
puts d.x # => undefined method `x' for ... (NoMethodError) | |
puts d.instance_variable_get :@x # => 10 |
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
module T | |
@@foo = 'bar' | |
def self.set(x) | |
@@foo = x | |
end | |
def self.get | |
@@foo | |
end |
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 Base | |
def initialize() | |
@x = 10 | |
end | |
end | |
class Derived < Base | |
def x | |
@x = 20 | |
end |
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
module ToFile | |
def filename | |
"object_#{self.object_id}.txt" | |
end | |
def to_f | |
File.open(filename, 'w') {|f| f.write(to_s)} | |
end | |
end | |
class Person | |
include ToFile |
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
'begin' <=> 'end' # => -1 | |
'same' <=> 'same' # => 0 |
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 SizeMatters | |
include Comparable | |
attr :str | |
def <=>(anOther) | |
str.size <=> anOther.str.size | |
end | |
def initialize(str) | |
@str = str | |
end | |
def inspect |