Created
May 7, 2013 07:17
-
-
Save sunnylost/5530802 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
class MyLogger | |
private_class_method :new | |
@@logger = nil | |
def MyLogger.create | |
@@logger = new unless @@logger | |
@@logger | |
end | |
end | |
=begin | |
log1 = MyLogger.create | |
log2 = MyLogger.create | |
puts log1.object_id | |
puts log2.object_id | |
=end | |
class Accounts | |
def initialize(checking, savings) | |
@checking = checking | |
@savings = savings | |
end | |
private | |
def debit(account, amount) | |
account.balance -= amount | |
end | |
def credit(account, amount) | |
account.balance += amount | |
end | |
public | |
def transfer_to_savings(amount) | |
debit(@checking, amount) | |
credit(@savings, amount) | |
end | |
end | |
class Account | |
attr_reader :cleared_balance | |
protected :cleared_balance | |
def greater_balance_than(other) | |
return @cleared_balance > other.cleared_balance | |
end | |
end | |
# a string can be frozen | |
a = "s" | |
a.freeze | |
# Singleton method | |
class SingletonTest | |
def info | |
puts 'This is a singleton method' | |
end | |
def meth1 | |
puts 'This is method1' | |
end | |
def meth2 | |
puts 'This is method2' | |
end | |
end | |
o1 = SingletonTest.new | |
o2 = SingletonTest.new | |
class << o2 | |
def meth1 | |
puts "This is o2's method1" | |
end | |
def meth2 | |
puts "This is o2' method2" | |
end | |
end | |
#o1.meth1 | |
#o2.meth1 | |
# class variable | |
class Person | |
@@number = 0 | |
def Person.getNumber_1 | |
@@number | |
end | |
# in Ruby, all the classes are objects too. So they can be treaed like Class's instance | |
def self.getNumber_2 | |
@@number | |
end | |
class << Person | |
def getNumber_3 | |
@@number | |
end | |
end | |
class << self | |
def getNumber_4 | |
@@number | |
end | |
end | |
end | |
#puts Person.getNumber_1 | |
#puts Person.getNumber_2 | |
#puts Person.getNumber_3 | |
#puts Person.getNumber_4 | |
# redefine class's method | |
=begin | |
class RedefTest | |
def meth | |
puts 'This is meth' | |
end | |
end | |
RedefTest.new.meth | |
class RedefTest | |
def meth | |
puts 'This is new meth' | |
end | |
end | |
RedefTest.new.meth | |
class RedefTest | |
undef_method :meth | |
end | |
RedefTest.new.meth | |
=end | |
# when declare a class variable in module, all classes which include this module can access this variable | |
module TestModule | |
@@foo = 10 | |
end | |
class Klass | |
include TestModule | |
p @@foo += 1 | |
end | |
class Base | |
include TestModule | |
p @@foo += 1 | |
end | |
# access a static variable from a class or module | |
class Meth | |
PI = 3.14 | |
end | |
def circle_area(arg) | |
Math::PI * arg * arg | |
end | |
p circle_area 5 | |
class Object | |
STATIC = 123 | |
end | |
p ::STATIC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment