Ruby metaprogramming meta Programming

Source: Internet
Author: User
module A;end
module B; include A;def hi; puts "Hello gays! enjoy Ruby! ";end; end
class C; include B; end

$f = File.open("metaProgram.rb")
$lines = $f.readlines

def p(a,no)
#$f.readline until $f.lineno == (no-1)
#puts "#{a}------>->->#{$f.readline}"

puts "#{a}------>->->#{$lines[no-1]}"
end

p(C < B,__LINE__)
p( B < A,__LINE__)
p( C < A,__LINE__)
p( Fixnum < Integer,__LINE__)
p( Integer < Comparable,__LINE__)
p( Integer < Fixnum,__LINE__)
p( String < Numeric,__LINE__)

p( A.ancestors,__LINE__)
p( B.ancestors,__LINE__)
p( C.ancestors,__LINE__)
p( String.ancestors,__LINE__)

p( C.include?(B),__LINE__)
p( C.include?(A),__LINE__)

p(A.class,__LINE__)
#p(A.superclass,__LINE__)
p(B.class,__LINE__)
#p(B.superclass,__LINE__)
p(C.class,__LINE__)
p(C.superclass,__LINE__)

p(A.included_modules,__LINE__)
p(B.included_modules,__LINE__)
p(C.included_modules,__LINE__)

p(($f.instance_of? A),__LINE__)
p(($f.instance_of? File),__LINE__)

p(($f.kind_of? File),__LINE__)
p(($f.is_a? File),__LINE__)

#----------------extend-------------

#B.new.hi

s = "String object "
s.extend(B)
p s.hi,__LINE__

M = Module.new{def mod
puts "called from #{self.class} , defined in M.mod"
end
}

D = Class.new{
def cl
puts "called from #{self.class} , defined in D.cl"
end
}

E = Class.new(D)\
{
include M

def hello
puts "called from #{self.class} , defined in E.hello"
end
}

#D.new.mod
D.new.cl
E.new.mod
E.new.cl
E.new.hello

#--------------eval--------------
xa = 1
puts eval "xa+1"

#-------------Binding--------------
class Object
def bindings
binding
end
end

class Test
def initialize(x)
@x = x
end
end

t = Test.new(10)
puts eval("@x",t.bindings) #oh! shit! you can pick the private member

#--------------------------------

t.instance_eval("@x")
String.class_eval("def len;size;end")
String.class_eval("alias len size")
String.instance_eval("def empty; ' ' ; end ")

s = "how long is this" ;
puts s.len #Noctice:class_eval() is for instance
puts s.size # I feel confused
puts "|#{String.empty}|" #Notice: instance_eval() is for Class

#-------------instance_exec/class_exec---------------


#----------var and Constrans------------
puts global_variables
x=1
puts local_variables

#define a simple class
class Point
def initialize(x,y)
@x =x
@y = y
end
@@class_var =1
ORIGIN = Point.new(0,0)
end

p(Point::ORIGIN.instance_variables,__LINE__)
p(Point::class_variables,__LINE__)
p(Point::constants,__LINE__)

p( "s".method(:reverse),__LINE__)
p String.instance_method(:reverse),__LINE__


p(("hello".send :upcase),__LINE__)
p(Math.send(:sin,Math::PI/2),__LINE__)

#the class_eval() is to use the private method ---define_method()
#It is really powerful
String.class_eval{define_method(:greet,lambda{puts "hello"})}

"S".greet

  

The output is as follows:

True ------>-> P (C <B ,__ line __)

True ------>-> P (B <a ,__ line __)

True ------>-> P (C <a ,__ line __)

True ------>-> P (fixnum <integer ,__ line __)

True ------>-> P (integer <comparable ,__ line __)

False ------>-> P (integer <fixnum, __line __)

------>-> P (string <numeric, __line __)

A ------>-> P (A. ancestors ,__ line __)

Ba ------>-> P (B. ancestors ,__ line __)

Cbaobjectkernel ------>-> P (C. ancestors ,__ line __)

Stringenumerablecomparableobjectkernel ------>-> P (string. ancestors, __line __)

True ------>-> P (C. Include? (B) ,__ line __)

True ------>-> P (C. Include? (A) ,__ line __)

Module ------>-> P (A. Class ,__ line __)

Module ------>-> P (B. Class ,__ line __)

Class ------>-> P (C. Class ,__ line __)

Object ------>-> P (C. superclass ,__ line __)

------>-> P (A. included_modules ,__ line __)

A ------>-> P (B. included_modules ,__ line __)

Bakernel ------>-> P (C. included_modules ,__ line __)

False ------>-> P ($ F. instance_of? A) ,__ line __)

True ------>-> P ($ F. instance_of? File) ,__ line __)

True ------>-> P ($ F. kind_of? File) ,__ line __)

True ------>-> P ($ F. is_a? File) ,__ line __)

Hello gays! Enjoy Ruby!

------>-> P s. Hi ,__ line __

Called from D, defined in D. cl

Called from E, defined in M. Mod

Called from E, defined in D. cl

Called from E, defined in E. Hello

2

10

16

16

|

$-W

$:

$.

$ Kcode

$-F

$ *

$ Stderr

$,

$'

$-P

$"

$

$ <

$ @

$-V

$-I

$ Deferr

$ \

$ =

$;

$ Program_name

$ Stdout

$ &

$-D

$ Load_path

$-

$ Verbose

$ Filename

$ Defout

$-0

$ +

$ Lines

$0

$ Stdin

$ ~

$ Debug

$-I

$ _

$-K

$>

$/

$'

$ F

$-L

$ Loaded_features

$?

$ Safe

$!

S

XA

T

X

@ X @ Y ------>-> P (point: origin. instance_variables, __line __)

@ Class_var ------>-> P (point: class_variables ,__ line __)

Origin ------>-> P (point: constants ,__ line __)

# <Method: string # reverse> ------>-> P ("S". Method (: reverse) ,__ line __)

# <Unboundmethod: string # reverse> ------>-> P string. instance_method (: reverse) ,__ line __

Hello ------>-> P ("hello". Send: upcase) ,__ line __)

1.0 ------>-> P (math. Send (: sin, Math: PI/2) ,__ line __)

Hello

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.