ruby 笔记

1. 与静态语言不同,每个对象(属于同一个类的)的实例变量不一定相同,只有给实例变量赋值时才开始存在 通过 instance_variables 获取所有实例变量。

2.methods 方法获取对象的所有方法。String.instance_method=="hello".methods

3.对象的方法为其类的实例方法,类的方法为Class的实例方法。

4.大写为常量,包括类名、模块名。类是对象(Class的实例),类是模块

5.ruby解释器创建一个main对象作为顶级上下文,最开始的self即main对象。
[img]http://dl.iteye.com/upload/attachment/0071/1793/47222362-9343-3793-a6cf-329c68a14f0c.jpg[/img]
Object 包含方法数量之多,慢慢研究吧。
[:to_s,
:public,
:private,
:include,
:context,
:conf,
:irb_quit,
:exit,
:quit,
:irb_print_working_workspace,
:irb_cwws,
:irb_pwws,
:cwws,
:pwws,
:irb_current_working_binding,
:irb_print_working_binding,
:irb_cwb,
:irb_pwb,
:irb_chws,
:irb_cws,
:chws,
:cws,
:irb_change_binding,
:irb_cb,
:cb,
:workspaces,
:irb_bindings,
:bindings,
:irb_pushws,
:pushws,
:irb_push_binding,
:irb_pushb,
:pushb,
:irb_popws,
:popws,
:irb_pop_binding,
:irb_popb,
:popb,
:source,
:jobs,
:fg,
:kill,
:help,
:irb_exit,
:irb_context,
:install_alias_method,
:irb_current_working_workspace,
:irb_change_workspace,
:irb_workspaces,
:irb_push_workspace,
:irb_pop_workspace,
:irb_load,
:irb_require,
:irb_source,
:irb,
:irb_jobs,
:irb_fg,
:irb_kill,
:irb_help,
:nil?,
:===,
:=~,
:!~,
:eql?,
:hash,
:<=>,
:class,
:singleton_class,
:clone,
:dup,
:initialize_dup,
:initialize_clone,
:taint,
:tainted?,
:untaint,
:untrust,
:untrusted?,
:trust,
:freeze,
:frozen?,
:inspect,
:methods,
:singleton_methods,
:protected_methods,
:private_methods,
:public_methods,
:instance_variables,
:instance_variable_get,
:instance_variable_set,
:instance_variable_defined?,
:instance_of?,
:kind_of?,
:is_a?,
:tap,
:send,
:public_send,
:respond_to?,
:respond_to_missing?,
:extend,
:display,
:method,
:public_method,
:define_singleton_method,
:object_id,
:to_enum,
:enum_for,
:==,
:equal?,
:!,
:!=,
:instance_eval,
:instance_exec,
:__send__,
:__id__
]


Class包含一些特有的方法
[img]http://dl.iteye.com/upload/attachment/0071/1864/9db42d4f-fcf0-3e53-94df-f7c79369dc46.jpg[/img]
[:allocate,
:new,
:superclass,
:freeze,
:===,
:==,
:<=>,
:<,
:<=,
:>,
:>=,
:to_s,
:included_modules,
:include?,
:name,
:ancestors,
:instance_methods,
:public_instance_methods,
:protected_instance_methods,
:private_instance_methods,
:constants,
:const_get,
:const_set,
:const_defined?,
:const_missing,
:class_variables,
:remove_class_variable,
:class_variable_get,
:class_variable_set,
:class_variable_defined?,
:public_constant,
:private_constant,
:module_exec,
:class_exec,
:module_eval,
:class_eval,
:method_defined?,
:public_method_defined?,
:private_method_defined?,
:protected_method_defined?,
:public_class_method,
:private_class_method,
:autoload,
:autoload?,
:instance_method,
:public_instance_method,
:nil?,
:=~,
:!~,
:eql?,
:hash,
:class,
:singleton_class,
:clone,
:dup,
:initialize_dup,
:initialize_clone,
:taint,
:tainted?,
:untaint,
:untrust,
:untrusted?,
:trust,
:frozen?,
:inspect,
:methods,
:singleton_methods,
:protected_methods,
:private_methods,
:public_methods,
:instance_variables,
:instance_variable_get,
:instance_variable_set,
:instance_variable_defined?,
:instance_of?,
:kind_of?,
:is_a?,
:tap,
:send,
:public_send,
:respond_to?,
:respond_to_missing?,
:extend,
:display,
:method,
:public_method,
:define_singleton_method,
:object_id,
:to_enum,
:enum_for,
:equal?,
:!,
:!=,
:instance_eval,
:instance_exec,
:__send__,
:__id__]

6. 方法可见性,public、private、protected是[color=red]Module类[/color]的实例方法。
Class、Module的实例方法可在普通类定义中直接使用。

7. 对象创建,new是Class类的实例方法。普通类名是Class的常量对象,所以可以用 【普通类名.new】的方式创建对象。

8. 类创建。 Class类还有一个类方法也叫new。 Class.new 用于动态创建新的类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值