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

简介: • 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.

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目录的内容

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

 

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

 

相关文章
|
4月前
|
缓存 关系型数据库 数据库
上云一年烧320万美元,成功“下云”六个月后Ruby on Rails之父现身回了 14 个问题!...
上云一年烧320万美元,成功“下云”六个月后Ruby on Rails之父现身回了 14 个问题!...
25 1
|
5月前
|
SQL 前端开发 数据库
前端学 Ruby:熟悉Rails
前端学 Ruby:熟悉Rails
166 0
|
5月前
|
前端开发 Linux 数据库
前端学 Ruby:安装Ruby、Rails
前端学 Ruby:安装Ruby、Rails
74 0
|
7月前
|
Java Linux 应用服务中间件
在 Ruby on Rails 应用程序中重用 Java 代码
在 Ruby on Rails 应用程序中重用 Java 代码
|
9月前
|
存储 前端开发 关系型数据库
Ruby on Rails 快速入门2
Ruby on Rails 快速入门2
126 0
|
9月前
|
移动开发 前端开发 关系型数据库
Ruby on Rails 快速入门1
Ruby on Rails 快速入门1
82 0
|
机器学习/深度学习 开发框架 Java
别梦依稀咒逝川,Ruby二十八年前|M1芯片Mac os系统配置Ruby(3.0.0) on Rails(6.1.1)开发环境
在每个开发者心里,都会有一门“最好”的语言,在这个世界的某个深处,在一些矫矫不群的人们心中,这门语言的名字叫做Ruby,它今年二十八岁了,历史和Java一样的悠久,但是它没有大厂背书、它的性能被开发者诟病、时至今日依然无法高效利用多核资源,甚至于它每年都要被“死亡”一次,相比于有太阳计算机系统、甲骨文、IBM 这些大公司支持的 Java,它是那么的一无所有,但是,它又拥有全世界最虔诚的“信徒”,拥有最活跃的开发者社区,这一切,又让它是那么的应有尽有。是的,这就是Rubyist的理念:有的时候,你想证明给一万个人看,到后来,你发现只得到了一个明白的人,那就够了。
别梦依稀咒逝川,Ruby二十八年前|M1芯片Mac os系统配置Ruby(3.0.0) on Rails(6.1.1)开发环境
|
JavaScript 前端开发 Java
基于Docker在Win10平台搭建Ruby on Rails 6.0框架开发环境
2023年,“非著名Web框架”--Ruby on Rails已经18岁了。在今年,Rails 6.0趋于完善,除了拿掉讨厌的Jquery,Webpacker 也成为默认前端打包方案,Sprockets 开始软着陆,未来很可能会和Jquery一样被彻底废弃,这就是历史的进程。
基于Docker在Win10平台搭建Ruby on Rails 6.0框架开发环境
|
开发框架 安全 Ruby
CVE-2018-3760 Ruby On Rails 路径穿越漏洞
CVE-2018-3760 Ruby On Rails 路径穿越漏洞
167 0
CVE-2018-3760 Ruby On Rails 路径穿越漏洞
|
安全 Ruby
CVE-2019-5418 Ruby on Rails 路径穿越与任意文件读取漏洞
CVE-2019-5418 Ruby on Rails 路径穿越与任意文件读取漏洞
181 0
CVE-2019-5418 Ruby on Rails 路径穿越与任意文件读取漏洞