• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Ruby Rails 笔记 [rails for dot net developers节选]

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

In Ruby’s object-oriented world, we work with objects and methods. Unlike VB .NET, where some subroutines return a value (Functions) and others do not (Subs), all Ruby methods must return a value. When an explicit return statement is not used, the last-evaluated expression automatically becomes the return value.

Variables are not declared prior to their use. Ruby automatically allocates memory for variables upon first use, and it also assigns their type based on inference. Like .NET, a garbage collector will reclaim memory automatically.

• There is no distinction made between methods, properties, and fields (or “member variables”) like there is in .NET. Ruby has only the concept of methods. However, Ruby classes do sport a convenient syntax for defining “attribute methods,” which are equivalent to defining .NET properties. attr_reader and attr_accessor automatically define methods that provide property-like access to instance variables.

Comments start with the hash character (#), unless the hash occurs inside a double-quoted string. Everything after the hash is ignored. There is no multiline comment character in Ruby.

• Ruby classes may define instance methods and class methods. Class methods are called static methods in .NET.

Methods can be declared public, protected, or private, and these visibility scopes have the same meaning as they do in .NET. There is no Ruby equivalent for “internal” or “assembly-level” visibility.

• Instance variable names must start with an at (@) sign and are always private. Class variables, or what we might call static variables in .NET, start with two at signs. The rules for memory allocation and object assignment for class variables can get pretty strange in Ruby, so we tend to avoid using them, especially since Rails provides an alternative syntax for using class variables in Rails applications.

• Ruby classes can be derived from only one base class but can“mix in” any number of modules. A module in Ruby is simply a set of related methods packaged together using the module key-word instead of class.

• There is no separate compilation step in Ruby. If we execute this Ruby code:

name = 'Joe'

len = name.length

puts name + " has " + len.to_s + " letters in his name."

puts "\t#{5*10}"

puts "Hello, #{name}. You have #{name.length} letters in your name"

#Searching and Replacing

word = "restaurant"

puts word.index('a') # prints 4

puts word.index("ant") # prints 7

puts word.index(/st.+nt$/) # prints 2

puts word.index(/ANT$/i) # prints 7

puts word.index('buffet') # prints "nil"

flight = "United Airlines, Flight #312, ORD to LAX, 9:45AM to 11:45AM"

puts flight.sub('United', 'American')

puts flight.sub(/(\w+)to/, 'PDX to')

puts flight.gsub('AM', 'PM')

#Trimming Whitespace

flight = " United Airlines, Flight #312, 9:45AM to 11:45AM "

flight = flight.gsub(/^\s+/, '') # remove leading whitespace

flight = flight.gsub(/\s+$/, '') # remove trailing whitespace

flight = flight.strip # removes leading and trailing whitespace

 

开发环境

使用命令行即可完成开发,如果需要代码导航、函数定义列表、重构等更高的效率,使用IDE是更佳的选择

Ruby in steel

商业软件[60天免费试用]

基于VS的插件

Aptana RadRails

开源软件

基于Eclipse

API和接口参考、图书

rdoc

命令行输入:gem server

在http://localhost:8808可以查看安装的rdoc文档

rails手册

www.railsbrain.com 可下载或在线查看

查找起来比rdoc更方便[IE无法察看时使用FireFox]

图书

Programming Ruby中文版

Agile web development with rails 3rd edition

rails for dot net developers

开源项目参考

Redmine: http://www.redmine.org

安装

按照站点上的安装说明,配置即可运行[rack版本要符合]

分析

使用Radrails “Import”->”Existing Folder As New Project”,即可查看和分析

入口:

Config/routes.rb中定义了控制器的映射关系,如

map.home '', :controller => 'welcome’

ð App下的controllers views目录下welcome_controller.rb welcome目录的内容

这个工程涉及到较多的概念和相关处理,初步整明白这个估计基本都可以应用了

 

想了解更详细的内容,可参考图书

http://www.amazon.com/Rails-NET-Developers-Facets-Ruby/dp/1934356204/ref=la_B0034OUD06_1_1?ie=UTF8&qid=1343220323&sr=1-1

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap