1. load_path, require, load, include
load_path相当于java的class path
require和load相当于java的import,但load和require是有区别的。
1. require不需要指定文件后缀,而load需要。
2. require会track所有已经require的文件,不会对同一个文件require两次。而load每次都会重新load文件。
那么load用在哪里呢?在有些场合load是有用的,比如rails的developmeng环境,每次都需要load最新的文件,以保证程序运行在最新的代码之上。
include用于做mixin。
2. namespace
无论如何,要避免conflict的namespace。
Ruby Namespace Conflicts
3. block
class JukeboxButton < Button
def initialize(label, &action)
super(label)
@action = action
end
def buttonPressed
@action.call(self)
end
end
bStart = JukeboxButton.new("Start") { songList.start }
bPause = JukeboxButton.new("Pause") { songList.pause }
注意initialize方法的最后一个参数:如果一个方法的最后一个参数前面带有&符号,那么当方法被调用时,会去寻找是否有一个block可
以被调用。注意到在initialize方法中,block被转化成一个Proc对象,赋值给@action实例变量。当buttonPressed方法
被调用时,会调用@action的call方法,以调用这个Proc对象。
Block或者Proc对象不仅仅是一团代码,跟这个block(当然proc对象也一样)关联的还有所有这个block定义之处的所有上下文:self,方法,变量等等。
4. method_missing
When you send a message to an object, the object executes the first method it finds on its method lookup path
with the same name as the message. If it fails to find any such method, it raises a NoMethodError
exception - unless you have provided the object with a method called method_missing
. The method_missing
method is passed the symbol of the nonexistent method, and any arguments that were passed in.
这就是machinist里Sham的实现方式。要注意的是,如果你想捕获的是class method,那么你要定义一个self.method_missing方法。
5. public protected private
With no arguments, sets the default visibility for subsequently defined
methods to public
. With arguments, sets
the named methods to have public
visibility.
Many things in Ruby is just method
!
ruby永远不想真的限制你做什么事情,对于protected或者private方法的调用只需object.send(:method_name)即可。
6. What's the binding
Objects of class Binding
encapsulate
the execution context at some particular place in the code and retain this
context for future use. The variables, methods, value of self
, and
possibly an iterator block that can be accessed in this context are all
retained. Binding
objects can be created using
Kernel#binding
, and are made
available to the callback of Kernel#set_trace_func
.
These binding objects can be passed as the second argument of the Kernel#eval
method, establishing an
environment for the evaluation.
class Demo
def initialize(n)
@secret = n
end
def getBinding
return binding()
end
end
k1 = Demo.new(99)
b1 = k1.getBinding
k2 = Demo.new(-3)
b2 = k2.getBinding
eval("@secret", b1) #=> 99
eval("@secret", b2) #=> -3
eval("@secret") #=> nil
7. Proc
Proc
objects are blocks of code that have
been bound to a set of local variables. Once bound, the code may be called
in different contexts and still access those variables.
def gen_times(factor)
return Proc.new {|n| n*factor }
end
times3 = gen_times(3)
times5 = gen_times(5)
times3.call(12) #=> 36
times5.call(5) #=> 25
times3.call(times5.call(4)) #=> 60
8. system
Executes cmd
in a subshell
, returning true if the command
was found and ran successfully, false otherwise. An error status
is available in $?. The arguments are processed in the same way as
for Kernel::exec.
system("echo *")
system("echo", "*") # echo "*"
produces:
config.h main.rb
*
9. `
Returns the standard output
of running cmd
in a subshell. The
built-in syntax %x{…} uses this method. Sets $? to
the process status.
`date` #=> "Wed Apr 9 08:56:30 CDT 2003\n"
`ls testdir`.split[1] #=> "main.rb"
`echo oops && exit 99` #=> "oops\n"
$?.exitstatus #=> 99
10. *
class File
def File.openAndProcess(*args)
f = File.open(*args)
yield f
f.close()
end
end
openAndProcess方法的参数*args意味着:把传入的参数集合成一个数组。
而当调用File.open时,传入*args作为参数,意味着:把传入的数组(args)展开。
11. block_given?
可以用block_given?来做条件判断:
class File
def File.myOpen(*args)
aFile = File.new(*args)
# If there's a block, pass in the file and close
# the file when it returns
if block_given?
yield aFile
aFile.close
aFile = nil
end
return aFile
end
end
同时,对于有yield的方法,如果不传block会出错。所以,可以用block_given?来设条件。
12. sort
files = Dir["*"]
sorted = files.sort {|a,b| File.new(a).mtime <=> File.new(b).mtime}
# so that we can overide one's <=> method to change the sort behaivor.
equals to:
sorted = Dir["*"].sort_by {|f| File.new(a).mtime }
14. File.dirname(__FILE__)
取得当前文件所在的目录
15. Freeze
Prevents further modifications to obj
. A TypeError
will be raised if modification is
attempted. There is no way to unfreeze a frozen object.
16.
class A
VALUE = A.new // it will fail because when run this line, class A is not yet loaded. This is quite
// different with Java.
end
17. ruby的builder库
xmlMarkup,很好地结合使用了block和method_missing.
18. BigDecimal
jruby也有BigDecimal类。
19. Class Variable and Instance Variable
我们对Ruby的class variable和instance variable估计都很熟悉,但可能下面的程序输出结果有点小小出乎你的意料:
class ExampleClass
@@variable = 'class variable'
@variable = 'class instance variable'
def initialize
@variable = 'instance varialbe'
end
def self.test
p @@variable
end
end
example = ExampleClass.new
example.instance_eval do
p @variable
end
ExampleClass.instance_eval do
p @variable
end
ExampleClass.test
p example.instance_variables.inspect
p ExampleClass.instance_variables.inspect
p ExampleClass.class_variables.inspect
output:
"instance variable"
"class instance variable"
"class variable"
"["@variable"]"
"["@variable"]"
"["@@variable"]"
对于class variable和instance variable的区别大家都很清楚。而疑惑点在于“instance variable”和“class instance variable”之间的区别。疑惑的本质还在于对ruby对象模型的不清晰。其实,类ExampleClass本身也是一个对象,它是Class的一个实例。而class instance variable是ExampleClass这个实例的instance variable。
http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes
20. extend vs include
http://codeprairie.net/blogs/chrisortman/archive/2007/07/07/ruby-include-and-extend.aspx
分享到:
相关推荐
此Ruby on Rails应用程序允许管理ACP(邻近合同农业)。 当前,以下PCA使用它: 支持的功能包括: 成员资格管理(状态,缺勤等) 订阅管理(篮子的类型,存放地点,数量等) 管理其他购物篮(交货频率,数量等)...
MailyHerald是Ruby on Rails的瑰宝,可帮助您发送和管理应用程序邮件。 将Maily视为自托管的Mailchimp替代方案,您可以轻松地将其集成到您的站点中。 MailyHerald既适用于电子邮件营销,也可以处理您发送给用户的...
该项目将每周开发一次,每周引入新概念。 启动并运行 克隆此项目后,导航到终端/命令提示符中的项目目录,然后键入命令: bundle install 这将安装运行项目所需的所有 gem。 之后,您可以启动服务器: rails ...
每周发布 1 个 15 分钟的演员表以及 2 分钟的 Ruby 或 2 分钟的 Rails 截屏视频!卫士::厨师 Chef guard 允许自动和智能地更新厨师的角色、食谱和数据包。 与 Bundler 1.0.x 兼容在 Ruby 1.8.6、1.8.7 和 1.9.2 上...
该网络应用程序每周在上发送项目通讯。 使用的技术 Ruby2.7.2带导轨6.1.3 :sailboat: 用于数据库的postgresql和redis :books: 用于测试的rspec :robot: aws ses,用于smtp服务器 :open_mailbox_with_raised_flag:...
pinterest_clone, 在 12周的挑战中,我的12网络应用程序每周 4 Pinterest网络应用我的中的第 12周,挑战为 Challenge我的目标是在每周 12周内建立一个新的web应用程序( 使用 Rails ) 来教自己的Ruby on Rails 。...
blog, 在 12周的挑战中,我的12网络应用程序每周 2 我的中的第 12周,挑战为 Challenge我的目标是在每周 12周内建立一个新的web应用程序( 使用 Rails ) 来教自己的Ruby on Rails 。这周我建立了一个简单的博客,我...
Rails贴花加州大学伯克利分校的Rails Decal 2015年秋季回购。railsdecal.com所有每周项目。
Ironhack 练习和每周工作。第 0 周在您的计算机上设置开发环境以使用 Ruby 和 Rails 进行 Web 开发能够创建基于 HTML5 的网页了解如何使用 Google Chrome 团队提供给您的秘密武器来调试 CSS 问题使用 Unix 命令行...
每周发布 1 个 15 分钟的演员表以及 2 分钟的 Ruby 或 2 分钟的 Rails 截屏视频! 纸牌 该库可让您访问一副实用的纸牌和一副 21 点纸牌。 甲板可以用鞋码初始化 安装 将此行添加到应用程序的 Gemfile 中: gem ...
类记录器 将多个记录器或自定义记录器添加到任何 ruby 类。 主要是为了让 Rails 中的某些模型记录到不同的文件,同时维护原始记录器(或覆盖它)。 这个想法来自于和他的 class_logger。 我只是增加了一些灵活性...
系统每周向所有用户发送一封电子邮件:(请参阅apps / jobs / weekly_stats_job.rb) 上周发送和接收了多少条消息 自用户发送上一条消息以来收到的消息总数 设置 安装Ruby 2.7.1 gem install bundler bundle ...
每周跟踪转化情况 监控热门搜索的效果 适用于任何搜索平台,包括Elasticsearch,Sphinx和Solr :heart_with_arrow: 伴侣 安装 将此行添加到应用程序的Gemfile中: gem "searchjoy" 并运行发电机。 这将创建迁移以...
电影分级和评论应用程序 我的12中12挑战赛的5周 我面对这个挑战的目标是在12周内每周构建一个新的Web应用程序(使用Rails),以自学Ruby on Rails。 本周,我构建了一个电影分级和审阅应用程序,它具有用户,电影,...
用Ruby on Rails编写。 贡献于令人敬畏的任务 请检查最新的母版,以确保尚未实现该功能或尚未修复该错误 检查问题跟踪器,确保没有人请求和/或提供它 分叉项目 启动功能/错误修正分支 承诺并推动,直到您对自己的...
每周任务一个Webapp,用于管理您每周必须处理的任务。... 框架,语言等Ruby 2.7.0 Ruby on Rails 5.1.7 Vue.js 威化Axios 规格AWS EC2 2020/07/31 修复了响应式设计错误2020/09/28 停止使用https 2020/11/14 完全停止
这个Rails Portfolio应用程序是为忙碌的父母打造的,他们没有时间为他们的孩子安排饭菜。 该应用程序将提供一个图表,使他们可以提前一整周计划用餐(早餐,午餐和晚餐)。 他们可以注册,登录,注销,创建图表,...
我在Cool Software Company实习,该公司是Ruby on Rails商店,并且我有很多辅助项目可以用来更好地理解Ruby。 该存储库包含这些项目的摘录,以及我的实习中经许可使用的一些代码。 体育联赛排名更新程序-对于本地...
乔布斯局 我12 12挑战赛第9周 我的目标是通过每周构建一个新的 Web 应用程序(使用 rails)来自学 Ruby on Rails,持续 12 周。 本周我构建了一个维基百科类型应用程序。 我们能够添加、编辑、销毁文章发布,并且每...
乔布斯局 我12 12挑战赛第7周 我的目标是通过每周构建一个新的 Web 应用程序(使用 rails)来自学 Ruby on Rails,持续 12 周。 本周我构建了一个 Jobs Board 应用程序。 我们能够添加、编辑、销毁职位发布,并且每...