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

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

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

原文地址:https://www.cnblogs.com/2018/p/2603505.html