转载自:
http://www.cnblogs.com/anshangcun/archive/2008/08/19/1270727.html
随笔 Rails 语法笔记
连接1 <%=link_to"goodMorning",:action=>"good"%>
连接2href="/homepage/good"> hello
前提是要有一个action good
<二>命名约定,Ruby框架会按命名约定到数据库中寻找
table name=users
file name=user.rb
class name=User
table name=site_users
file name=site_user
class name=SiteUser
<三> Rails maintains a special hash-like collection calledsession
<四>
||=
<五Ruby语法概要
Ruby语法概要(1)
Ruby语法概要(1)
(1)对象内省
song = Song.new()
song.inspect
(2)对象to string
song = Song.new()
song.to_s
(3)to_i
(4)super
如果无参数的情况下调用super,那么ruby将会调用父类的与当前方法同名的方法
(5)定义对象的读属性
class Song
def name
@name
end
def artist
@artist
end
end
class Song
attr_reader :name, :artist
end
(6)定义对象的写属性
class Song
def duration=(new_duration)
@duration = new_duration
end
end
class Song
attr_writer :duration
end
(7)class variables
class Song
@@plays = 0
end
(8)class method
class Song
def instance_method
end
def Song.class_method
end
end
(9)单例的实现
class MyLogger
private_class_method :new
@@logger = nil
def MyLogger.create
@@logger = new unless @@logger
end
end
(10)Protected修饰的方法
Protected methods can be invoked only by objects of defining class and its subclass.Acess is kept within the family.
If a method is protected,it may be called by any instance of the defing class or its subclass
(11)class中方法修饰的定义
class MyClass
def method1 #default is 'public'
#...
end
protected
def method2
#...
end
private
def method3
#...
end
public #subsequent methods will be 'public'
def method4
#...
end
end
(12)关于ruby中的引用
person1 = "Tim"
person2 = person1
person1[0] = 'J'
person1 -> "Jim"
person2 -> "Jim"
-----
person1 = "Tim"
person2 = person1.dup
person1[0] = "J"
person1 -> "Jim"
person2 -> "Tim"
-----
冻结对象的修改
person1 = "Tim"
person2 = person1
person1.freeze
person2[0] = "J" # can't modify frozen string(TypeError)
(13)数组的定义
a = [3.1415926,"pie",99]
a.class -> Array
a.length -> 3
a[3] -> nil
b = Array.new
b.class - > Array
b.length -> 0
b[0] = "second"
b[1] = "array"
(14)选取数组中的多个元素
[start,count]
a = [1,3,5,7,9]
a[1,3] -> [3,5,7]
a[-3,2]-> [5,7]
..包含结束的位置
...不包含结束的位置
a[1..3] -> [3,5,7]
a[1...3] -> [3,5]
(15)数组的赋值
如果是[num]=的形式,那么指定的位置将会被等号右边的值取代,空隙部分用nil取代
如果是[start,length]=的形式,等号右边的值将会添加在指定index之上,length表示需要擦除等号左边数组元素的数量
a = [1,3,5,7,9]
a[2,2] = 'cat' -> [1,3,'cat',9]
a[2,0] = 'dog' -> [1,3,'dog','cat',9]
a[1,1] = [9,8,7] ->[1,9,8,7,'dog','cat',9]
a[0..3] = [] ->['dog','cat',9]
a[5..6]=99,98 ->['dog','cat',9,nil,nul,99,98]
(16)Hash的赋值
h = {'dog'=>'canine','cat'=>'feline'}
h.length -> 2
h['dog'] -> "canine"
(17)遍历数组最直接的方法
class SongList
def with_title(title)
for i in [email protected]
return @songs[i] if title == @songs[i].name
end
return nil
end
end
(18)符合ruby风格的遍历数组的写法
class SongList
def with_title(title)
@songs.find {|song| title == song.name}
end
end
[1,3,5,7,9].each{|i| puts i}
(19)ruby中关于iterator中的定义
Iterator:a method that invokes a block of code repeatedly.
(20)方法中调用block
使用yield
(21)关于inject的用法
[1,3,5,7].inject(0){|sum.element| sum + element} ->16
第一次执行的时候会将0传入,将1传入element;第二次执行的时候会将第一次执行的结果1(0 + 1)传入sum,将第二个元素3传入element,依此类推,得到结果15
如果inject后面没有数字,如:
[1,3,5,7].inject{|sum.element| sum + element} ->16
如inject后没有数字,那么第一次其会将数组的第一个元素传入sum,将第二个元素传入element;第二次将第一次执行的结果传入sum,将第三个元素5传入element,依此类推,得到结果15
(22)判断在方法后是否跟有block
class File
def File.my_open(*args)
result = file = File.new(*args)
if block_given?
result = yield file
file.close
end
return result
end
(23){|???|???}定义的块和do..end定义的块
用do..end来定义一个块,这和用两个打括号定义一个块只是有优先级别的区别,具体还没看到
<六>
def self.salable_items
find(:all,
:conditions =>"date_available<=now()",
:order =>"date_available desc")
End
<七>model中的自定义方法 self
1,
class StoreController < ApplicationController
def index
@products=Product.salable_items
@productAll=Product.find(:all)
End
def self.salable_items
find(:all,
:conditions =>"date_available<=now()",
:order =>"date_available desc")
End
2,self.new
sometimes a class method needs to create an instance of that class
<八>链接
<% foroo in @productAll %>
<%=oo.id%>
<%=link_to 'Add To Cart',
:action=> 'add_to_cart' ,
:id=> oo %>
#并且这里有oo=oo.id,这个ID将作为action的参数传递给add_to_cart
#代码如下图的下方
<%end%>
在control中有一个add_to_cart的action
def add_to_cart
@abc="abc"
@product=Product.find(params[:id])
end
并且在view中有一个add_to_cart的rhtml
»博主下一篇: IDL实现的元胞自动机模型 Conway Life Game
· 叛变iPhone为哪般?
· 北大研究生发声明 抵制“苦力科研”
· Twitter联合创始人Ev Williams:何时卖掉你的公司?
· 科技无法挽回逝去的爱
· 微软动刀安卓阵营:向尼康相机平台收取专利费
» 更多新闻...
· 注重实效的架构师——大胆行前人未行之路
· 走向“持续部署”
· curl 网站开发指南
· 有关T-SQL的10个好习惯