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

Source: Internet
Author: User


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| putsi} (1..5). Each do |i|puts*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 (&:d owncase)>> ('a'..'Z'). To_a.shuffle[0..7].join#generate a random two-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***[email protected]**.com" }= {:name=>"Amysun",:email=>"12***[email protected]**.com"}>>User[:name]="Amysun">>user[:p Assword]=Nil>> h1={:name=>"Amysun",:email=>"12***[email protected]**.com" }= {:name=>"Amysun",:email=>"12***[email protected]**.com"}>> h1={Name:"Amysun", Email:"12***[email protected]**.com"}#:name=> and name: the same, so {:name=> "Amysun"} and {name: "Amysun"} are equivalent, but only: name represents the symbol= {:name=>"Amysun",:email=>"12***[email protected]**.com"}>> h1==H2= True

Nested hashes

>> params={}= {}>> params[:user]={Name:"Amysun", Email:"12***[email protected]**.com" }= {:name=>"Amysun",:email=>"12***[email protected]**.com"}>>params= {:user=>{:name=>"Amysun",:email=>"12***[email protected]**.com"}}>>Params[:user][:email]= 12***[email protected]**.com

Each method of hash

>> user={Success:"It worked!", Danger:"It failed." }= {:success=>"It worked!",:d anger=>"It failed."}user.each Do|key,value|puts"Key #{key.inspect} has value #{value.inspect}"End#Results#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.inspectname:name " It worked! ","It worked! " . Inspectit worked! " It worked! ">> p:name  # 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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: