Note one:
The code contains variables, classes, and methods, collectively referred to as language constructs (language construct).
# TEST.RB
class greeting
def initialize (text)
@text = text
end
def welcome
@text
End
end
my_obj = greeting.new ("Hello")
puts My_obj.class
puts My_obj.class.instance_methods (false) #false means not inherited puts My_obj.instance_variables result => greeting welcome
@ Text
Summarize:
An instance method inherits from a class, and an instance variable exists in the object itself.
Both classes and objects are the first class values in Ruby.
application Example:
MONGO API for Ruby => mongo::mongoclient # testmongo.rb require ' MONGO ' require '
pp '
include mongo< c5/># the members of Replcation-set
# Test MongoDB server version 2.6.0
host = "192.168.11.51"
# the Port of M Embers
# If The port is 27017 by default then Otherport don ' t need to assignment
Otherport = ""
port = Other Port.length!= 0? Otherport:mongoclient::D efault_port
opts = {:p ool_size => 5,:p ool_timeout =>}
# Create a new Connect Ion
client = mongoclient.new (host, Port, opts)
# puts Client.class
puts client.class.constants Puts Client.instance_variables
puts Client.class.instance_methods (false)
Output separately
Constant, Instance Attribute, Instance method
Note two: Dynamic invocation
When you call a method, you are actually sending a message to an object.
Class MyClass
def my_method (args)
args * end
obj = MyClass.New puts Obj.my_method
(5)
puts "* *"
puts Obj.send (: My_method, 6)
Results:
You can use the Object#send () instead of the dot marker to invoke the Myclass#my_method () method:
Obj.send (: My_method, 6)
The first parameter of the Send () method is the message to send to the object, either a symbol (: symbol) or a string, and other parameters are passed directly to the calling method.
You can dynamically decide which method to call technology, become dynamic Dispatch.
Note Three: The difference between a symbol and a string
1. Symbols immutable, you can modify the characters in the string.
2. Symbol-oriented operations are faster.
3. Usually the symbol is used to indicate the name of the thing.
For example:
Puts 1.send (: +, 4) => 5
string#to_sym (), String#intern () => String to Symbol
string#to_s (), string# Id2name () => symbol to string
"Caoqing". To_sym () =>: caoqing
: caoqing.to_s () => "caoqing"
Methods for distributing (pattern dispatch) using patterns in dynamic distribution.
Puts Obj.class.instance_methods (true). delete_if{|method_name| method_name!~/^my/} result
=>
My_method
Note four: dynamic definition
Use the Module#define_method () method to define a method.
Class MyClass
Define_method:my_method do |args|
args * 3
end
obj = myclass.new
puts Obj.my_method (10)
Results:30
A single piece method allows you to add a method to a single object. Singleton methods
# test.rb
str = "My name is caoqing."
def str.title?
Self.upcase = = Self End
puts Str.title?
Puts Str.methods.grep (/^title?/)
puts Str.singleton_methods
Results:
Note Five:
the nature of the class method, the class is an object, and the class name is a constant. Call methods and object invocation methods on a class:
Obj.my_method
Cla.class_method
Duck Typing: Whether the object can respond to a method, either a common method or a single piece of method.
The essence of the class method is that they are a single piece of the class method.
Obj can be an object reference, a constant class name, or self.
Classes macro (class Macro)
Ruby objects have no attributes, and you can use the Mimicry method to define properties.
A member of the Module#attr_* () method to define the accessor. A class macro is not a keyword but a method.
Eigenclass
a single piece method looks in the usual way to find where the ancestor chain cannot be found, obj is an object that cannot be saved, and cannot exist in class, otherwise all instances can share this method.
Object has a unique hidden class, called the Eigenclass of the object.
Enter Eigenclass scope:
If you want to get a reference to Eigenclass, you can return self when you leave the scope:
Appendix:
class variables, instance variables, class methods, instance method differences
@@ : Var class variable
@ : Instance variable
Self (? Clas,::) .method : Class method
method : Instance method
# TEST.RB
class Foo
@ @var = "Lion"
def self.method01
puts "cat"
@name = "Cat"
@ @var = "
Cat" Puts @name
end
def self.method02
puts "Tiger"
@name = "Tiger"
@ @var = "Tiger"
puts @name C14/>end
def self.method03
puts "dog"
@name = "dog"
@ @var = "Dog"
puts @name
end def putsname
puts @name
puts @ @var
end
obj = foo.new
# obj.method01 => ( Nomethoderror)
obj.putsname => Lion
foo.method01
foo.method02 foo.method03 Obj.putsname
Results:
Lion
cat
cat
Tiger
Tiger
dog
dog
Dog