Ruby on Rails Tutorial fourth the other data types of ruby behind Rails

Source: Internet
Author: User
Tags ruby on rails



1. Arrays and Ranges



2, Block



Convention: Use curly braces when there is only a single line of code in the block, when the block is a long line of code, or when there are many rows using do. End form. For example:


>> (1..5).each{|i| puts 2*i}
(1..5).each do |i|
     Puts 2*i
     Puts ‘-‘
End
>> 3.times{puts "betelgeuse"}
>> (1..5).map{|i| i**2}
>> %w[a b c].map{|char| char.upcase}
>> %w[A B C].map{|char| char.downcase}
>> %w[A B C].map(&:downcase)
>> (‘a‘..‘z‘).to_a.shuffle[0..7].join #Generate a random second-level domain name


3. Hashes and symbols



A hash is essentially an array, and its index is not limited to numbers. The index of the hash can use any object. A hash is represented by a pair of curly braces that contains some key-value pairs. The biggest difference between hashes and arrays is that hash elements do not have a specific order. Hash the value of a key that is not defined is nil


>> user={} #{} is an empty hash
>> user["first_name"]="Amy"
>> user["last_name"]="Sun"
>> user["first_name"]
=> "Amy"
>> user
=> {"first_name"=>"Amy","last_name"=>"Sun"}
>> user["e-mail"]
=> nil

>> user={ "first_name"=>"Amy","last_name"=>"Sun" }
=>{"first_name"=>"Amy","last_name"=>"Sun"}


symbol : A unique data type in Ruby, a string with no constraints


>> user={ :name=>"AmySun",:email=>"12***22@**.com" }
=> {:name=>"AmySun",:email=>"12***22@**.com"}
>> user[:name]
=> "AmySun"
>> user[:password]
=> nil

>> h1={ :name=>"AmySun",:email=>"12***22@**.com" }
=> {:name=>"AmySun",:email=>"12***22@**.com"}
>> h1={ name:"AmySun",email:"12***22@**.com" } #:name=> Same as name:, so { :name=>"AmySun" } and { Name: "AmySun" } is equivalent, but only: name indicates the symbol
=> {:name=>"AmySun",:email=>"12***22@**.com"}
>> h1==h2
=> true


Nested hashes


>> params={}
=> {}
>> params[:user]={ name:"AmySun",email:"12***22@**.com" }
=> {:name=>"AmySun",:email=>"12***22@**.com"}
>> params
=> {:user=>{:name=>"AmySun",:email=>"12***22@**.com"}}
>> params[:user][:email]
=> 12***22@**.com


Each method of hash


>> user={ success:"It worked!",danger:"It failed." }
=> {:success=>"It worked!",:danger=>"It failed."}
User.each do |key,value|
     Puts "Key #{key.inspect} has value #{value.inspect}"
End
# Result
# Key :success has value "It worked!"
# Key :failed has value "It failed."


Where the inspect method returns the string literal representation of the called object, for example:


>> puts (1..5).to_a.inspect
[1,2,3,4,5]
>> puts :name,:name.inspect
Name
:name
>> puts "It worked!","It worked!".inspect
It worked!
"It worked!"
>> p :name # is equivalent to ‘puts :name.inspect’
:name 


4. Introduction of CSS Instructions


' Application ', media:'all','data-trubolinks-track'=>true% >


Description: When calling a function, if the hash is the last argument, you can omit the curly brace, because Data-trubolinks-track has hyphens, so it cannot be written as Data-trubolinks-track: The true style generates HTML source code as follows:


<link data-trubolinks-track="true" href="/assets/application.css " media="alll" rel="stylesheet "/>







Ruby on Rails Tutorial fourth the other data types of ruby behind Rails


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.