ruby on rails 继续ing
来源:互联网 发布:boyd holbrook 知乎 编辑:程序博客网 时间:2024/06/06 03:20
ruby API http://ruby-doc.org/core-1.9.3/
rails API http://api.rubyonrails.org/v3.2.0/classes/ActionView/Helpers/AssetTagHelper/StylesheetTagHelpers.html#method-i-stylesheet_link_tag
0 (and everything other than nil and false itself) is true
>> "foo bar baz".split # Split a string into a three-element array
=> ["foo", "bar", "baz"] 将字符串转为array
>> "fooxbarxbazx".split('x')
=> ["foo", "bar", "baz"]
>> a = [42, 8, 17]
=> [42, 8, 17]
>> a[0] # Ruby uses square brackets for array access.
=> 42
>> a # Just a reminder of what 'a' is
=> [42, 8, 17]
>> a.first
42
感叹号的作用
>> a
=> [42, 8, 17]
>> a.sort
=> [8, 17, 42]
>> a.reverse
=> [17, 8, 42]
>> a.shuffle
=> [17, 42, 8]
>> a
=> [42, 8, 17]
>> a
=> [42, 8, 17]
>> a.sort!
=> [8, 17, 42]
>> a
=> [8, 17, 42]
>> a.push(6) # Pushing 6 onto an array
=> [42, 8, 17, 6]
>> a << 7 # Pushing 7 onto an array
=> [42, 8, 17, 6, 7]
>> a << "foo" << "bar" # Chaining array pushes
=> [42, 8, 17, 6, 7, "foo", "bar"]
join
>> a
=> [42, 8, 17, 7, "foo", "bar"]
>> a.join # Join on nothing
=> "428177foobar"
>> a.join(', ') # Join on comma-space
=> "42, 8, 17, 7, foo, bar"
>> a = %w[foo bar baz quux] # Use %w to make a string array.
=> ["foo", "bar", "baz", "quux"]
>> a[0..2]
=> ["foo", "bar", "baz"]
>> (0..9).to_a # Use parentheses to call to_a on the range
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> ('a'..'e').to_a
=> ["a", "b", "c", "d", "e"]
>> (1..5).each { |i| puts 2 * i }
>> (1..5).each do |i|
?> puts 2 * i
>> end
(1..5).map { |i| i**2 }
=> [1, 4, 9, 16, 25]
the map method returns the result of applying the given block to each element in the array or range.
hash用法
>> user = {} # {} is an empty hash.
=> {}
>> user["first_name"] = "Michael" # Key "first_name", value "Michael"
=> "Michael"
>> user["last_name"] = "Hartl" # Key "last_name", value "Hartl"
=> "Hartl"
>> user["first_name"] # Element access is like arrays.
=> "Michael"
>> user # A literal representation of the hash
=> {"last_name"=>"Hartl", "first_name"=>"Michael"}
>> user = { "first_name" => "Michael", "last_name" => "Hartl" }
symbo用法
>> user = { :name => "Michael Hartl", :email => "michael@example.com" }
=> {:name=>"Michael Hartl", :email=>"michael@example.com"}
{ name: "Michael Hartl", email: "michael@example.com" } //上面这两个是一样的
>> user[:name] # Access the value corresponding to :name.
=> "Michael Hartl"
>> user[:password] # Access the value of an undefined key.
=> nil
嵌套的hash symbol
> params = {} # Define a hash called 'params' (short for 'parameters').
=> {}
>> params[:user] = { name: "Michael Hartl", email: "mhartl@example.com" }
=> {:name=>"Michael Hartl", :email=>"mhartl@example.com"}
>> params
=> {:user=>{:name=>"Michael Hartl", :email=>"mhartl@example.com"}}
>> params[:user][:email]
=> "mhartl@example.com"
>> flash = { success: "It worked!", error: "It failed." }
=> {:success=>"It worked!", :error=>"It failed."}
inspect用法
puts "It worked!", "It worked!".inspectIt worked!"It worked!"
>> a = Array.new([1, 3, 2])
=> [1, 3, 2] 类实例化
自己修改String基类
>> class String
>> # Returns true if the string is its own reverse.
>> def palindrome?
>> self == self.reverse
>> end
>> end
=> nil
>> "deified".palindrome?
=> true
class User
attr_accessor :name, :email
def initialize(attributes = {})
@name = attributes[:name]
@email = attributes[:email]
end
def formatted_email
"#{@name} <#{@email}>"
end
end
In Rails, the principal importance of instance variables is that they are automatically available in the views, but in general they are used for variables that need to be available throughout a Ruby class.When we execute User.new. This particular initialize takes one argument, attributes。
如何引入一个类
>> require './example_user' # This is how you load the example_user code.
=> ["User"]
>> example = User.new
user = User.new(name: "Michael Hartl", email: "mhartl@example.com")
- ruby on rails 继续ing
- ruby on rails继续
- Ruby On Rails之继续乾坤大挪移
- 继续on Rails
- Ruby & Rails on Rails 進階書單
- ruby and ruby on rails
- install Ruby on Rails
- Ruby on Rails 实践
- Ruby on Rails!
- Ruby on Rails
- 关于Ruby on Rails
- 初探Ruby on rails
- Ruby on Rails
- 了解Ruby on Rails
- Ruby on Rails 是什么?
- 什么是Ruby on Rails
- Ruby on Rails 实践
- Ruby on Rails
- hdu~3501~欧拉函数
- 三星第三季度利润73亿美元 同比增长近100%
- unity3d模型制作规范v1.0
- VC6.0在win7环境下代码提示智能化
- php时区问题
- ruby on rails 继续ing
- 纹理过滤相关内容
- GridView和DataTable的数据提取
- poj 2411 Mondriaan's Dream / hdu 1400
- oracle10g数据库正则表达式
- vb6.0散架
- BT5安装后需要的更新源地址整理
- Windows应用程序的基本运行机制与HelloWin程序详细解
- 面试100题之我的解答