install nodejs
step:需要在root目录有一个package.json
使用脚手架
1.rails generate scaffold User name:string email:string
2.bundle exec rake db:migrate
访问
http://localhost:3000/users/new等可以CRUD
启动console查看各个资源之间的联系以及查找资源
first_user = User.first
first_user
.
microposts
rails console
二维数组:[[],[],[]]
- hash of Ruby:
hash = {
key1 => value1,
key2 => value2,
key3 => value3
} #preferred - pets = Hash.new <=> pets = {}
- pets["name"] = "Peter"
- access the key-value: puts pets["name"]
- family.each { |x, y| puts "#{x}: #{y}" }
- make the hash default value: a = Hash.new(“xufei")
- u
- 1)book_1 = "A Wrinkle in Time"
book_2 = "A Brief History of Time"
book_1 <=> book_2 #1
2)
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
# To sort our books in ascending order, in-place
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }
# Sort your books in descending order, in-place below
books.sort! { |firstBook, secondBook| secondBook <=> firstBook }
3)
def welcome
puts "Welcome to Ruby!"
end
welcome
4)def alphabetize(arr, rev=false)
arr.sort!
if rev
arr.reverse!
end
end
numbers=[1,2,3,6,5]
puts alphabetize(numbers, true)
5)symbol
puts "string".object_id
puts "string".object_id
puts :symbol.object_id
puts :symbol.object_id #colon my_first_symbol = :my_symbol
6)
:sasquatch.to_s
"sasquatch".to_sym
7)strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
# Add your code below!
symbols = []
strings.each do |s|
# sym = s.to_sym
sym = s.intern
symbols.push(sym)
end
8)相对于字符串作为key,这种symbol方式更有效率
movies = {
:movie1 => "bixu1”, #逗号
:movie2 => "bwxu1"
}
9)hash的进一步进化
new_hash = { one: 1,
two: 2,
three: 3
}
10)
movie_ratings = {
memento: 3,
primer: 3.5,
the_matrix: 5,
truman_show: 4,
red_dawn: 1.5,
skyfall: 4,
alex_cross: 2,
uhf: 1,
lion_king: 3.5
}
# Add your code below!
good_movies=movie_ratings.select {|k, v| v > 3} #等号两边不能有空格
11)
movie_ratings.each_key {|k| puts k}
12)
movies={
move_1: 3
}
puts "choose an operation please:"
choice=gets.chomp
case choice
when "add"
puts "input a movie title"
title=gets.chomp.to_sym
if movies[title].nil?
puts "input a the rating of this movie"
rating=gets.chomp.to_i
movies[title]=rating
puts "the movie title and its rating has been added.#{movies}"
else
puts "the movie title has already existed."
end
when "display"
movies.each do |k, v|
puts "#{k}: #{v}"
end
when "delete"
title=gets.chomp.to_sym
if movies[title].nil?
puts "the movies is not existing in the hash."
else
movies.delete(title) #注意delete方法,会改变object结构
end
when "update"
puts "please input the movie's name to update:"
title=gets.chomp.to_sym
if movies[title].nil?
puts "the movies is not existing in the hash."
else
puts "please input its rating:"
rating=gets.chomp.to_i
movies[title]=rating
end
else
puts "Error!"
end
11)if语句一行写,不用end
puts "fjslkdfla" if true
puts "fsdfs" unless false
12)
my_name ||= "lixufei"
13)
def multiple_of_three(n)
n % 3 == 0 ? "True" : "False"
end
multiple_of_three(3)
14)
95.upto(100) {|n| print n, " “} #
95 96 97 98 99 100
95
"L".upto("P") {|l| puts l}
15)
5.next #6
age.respond_to?(:next)
16)
[1, 2, 3] << 4
# ==> [1, 2, 3, 4]
17)
favorite_animal ||= "miumiu"
18) 得到前几个素数
prime = Prime.instance
prime.first n
19)
my_nums = [1, 2, 3]
my_nums.collect { |num| num ** 2 }
# ==> [1, 4, 9]
20)
fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
# Add your code below!
doubled_fibs = fibs.collect do |fib|
fib * 2
end
21)
def block_test
puts "We're in the method!"
puts "Yielding to the block..."
yield
puts "We're back in the method!"
end
block_test { puts ">>> We're in the block!" }
22)
def double(num)
yield(5)
end
double(3) {|n| n*2}
23)proc
floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]
# Write your code below this line!
round_down=Proc.new {|f| (f-0.5).round}
# Write your code above this line!
ints = floats.collect(&round_down)
--------------------
[1, 2, 3].collect!(&cube)
# ==> [1, 8, 27]
[4, 5, 6].map!(&cube)
# ==> [64, 125, 216]
(The
.collect!
and
.map!
methods do the exact same thing.)
--------------------------
24)
def greeter
yield
end
phrase=Proc.new {puts "Hello there!"}
greeter(&phrase)
25)
hi=Proc.new {puts "Hello!"}
hi.call
26)
numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
strings_array = numbers_array.map(&:to_s)
27)Lambda —think about the differences between lambda and proc;
def batman_ironman_proc
victor = Proc.new { return "Batman will win!" }
victor.call
"Iron Man will win!"
end
puts batman_ironman_proc
def batman_ironman_lambda
victor = lambda { return "Batman will win!" }
victor.call
"Iron Man will win!"
end
puts batman_ironman_lambda
27)
my_array = ["raindrops", :kettles, "whiskers", :mittens, :packages]
# Add your code below!
symbol_filter=lambda{|c| return c.is_a? Symbol}
symbols=my_array.select(&symbol_filter)
28)Camparison of block, proc, and lambda
block:
odds_n_ends = [:weezard, 42, "Trady Blix", 3, true, 19, 12.345]
ints=odds_n_ends.select{|a| a.is_a? Integer}
proc:
ages = [23, 101, 7, 104, 11, 94, 100, 121, 101, 70, 44]
# Add your code below!
under_100=Proc.new{|n| n<100}
youngsters=ages.select(&under_100)
lambda:
crew = {
captain: "Picard",
first_officer: "Riker",
lt_cdr: "Data",
lt: "Worf",
ensign: "Ro",
counselor: "Troi",
chief_engineer: "LaForge",
doctor: "Crusher"
}
# Add your code below!
first_half=lambda{|k, v| return v<"M"}
a_to_m=crew.select(&first_half)