•
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目錄的內容
這個工程涉及到較多的概念和相關處理,初步整明白這個估計基本都可以應用了
想瞭解更詳細的內容,可參考圖書
Rails for .NET Developers http://www.amazon.com/Rails-NET-Developers-Facets-Ruby/dp/1934356204/ref=la_B0034OUD06_1_1?ie=UTF8&qid=1343220323&sr=1-1