一直使用C#做开发,其实还不错,之前有学过python。用起来真没有C#顺手。但是未来开发网站,我不希望依靠C#,你懂的!最近做asp.net mvc 做一个商场 ,也有不爽的地方,希望积累行业经验后自己改用别的语言开发一个。需要一个能够肩负起开苏开发网站的技术,而语法上不会比C#逊色的,开源的,动态的语言来弥补。经过资料搜集,最后剩下clojure和Ruby,想到Ruby做网站开发的资源很多,就趁今天有时间u到 http://www.codecademy.com 学一下Ruby,题目 做到第9部分 。
Ruby写起来的确很顺手。而C#虽然强大 还是有很强的累赘感。
1
puts "What's your first name?"
first_name = gets.chomp
first_name.capitalize!
print "What's your last name?"
last_name = gets.chomp
last_name.capitalize!
print "What city are you from?"
city = gets.chomp
city.capitalize!
print "What state or province are you from?"
state = gets.chomp
state.upcase!
puts "Your name is #{first_name} #{last_name} and you're from #{city}, #{state}!"
2
print "Thtring, pleathe!: "
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
else
puts "Nothing to do here!"
end
puts "Your string is: #{user_input}"
3
for i in 1..10
puts i
end
4
i = 20
loop do
i -= 1
print "#{i}"
print ","
next if i % 2 != 0
break if i == 0
end
5
array.each do |x|
x += 10
print "#{x}"
end
6
puts "text:"
text = gets.chomp
words = text.split(" ")
h = Hash.new(0)
words.each{|w| h[w] +=1 }
h = h.sort_by{|x,y| y}
#h = h.reverse!
#h.each{|x,y| puts "#{x} : #{y}"}
puts h
7
frequencies = Hash.new(0)
"Take a look at the Hint if you need help.".split(" ")
.each{|x| frequencies[x]+=1}
puts frequencies
8
frequencies = Hash.new(0)
"TakealookattheHintifyouneedhelp."
.split(//)
.each{|x| frequencies[x]+=1}
frequencies = frequencies.sort_by{|x,y| y}
frequencies = frequencies.reverse!
frequencies.each{|x,y| puts "#{x} : #{y}"}
9
def prime(n)
unless n.is_a? Integer then puts "That's not an integer."
else
is_prime = true
for i in 2..n-1
if n % i == 0
is_prime = false
end
end
if is_prime
puts "#{n} is prime!"
else
puts "#{n} is not prime."
end
end
end
prime("sdaf")
prime(2)
prime(9)
prime(11)
prime(51)
prime(97)
10
def what_up(greeting, times = 2,*bros)
times.times{
print "#{greeting} "
bros.each_with_index {
|bro,index|
if index == 0 || index == bros.length
print "#{bro}"
else
print ",#{bro}"
end
}
}
end
what_up("What up",times = 1, "Justin", "Ben", "Kevin Sorbo")
11
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}
12
movies ={
top:"top",
"below"=>0,
1=>"b"
}
puts movies
puts movies[:top]
puts movies["below"]
puts "aa".to_sym
13
require 'benchmark'
string_AZ = Hash[("a".."z").to_a.zip((1..26).to_a)]
symbol_AZ = Hash[(:a..:z).to_a.zip((1..26).to_a)]
string_time = Benchmark.realtime do
100_000.times { string_AZ["r"] }
end
symbol_time = Benchmark.realtime do
100_000.times { symbol_AZ[:r] }
end
puts "String time: #{string_time} seconds."
puts "Symbol time: #{symbol_time} seconds."
14
movie_ratings = {
memento: 3,
primer: 3.5,
the_matrix: 3,
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.5}
puts good_movies
15
movie_ratings = {
memento: 3,
primer: 3.5,
the_matrix: 3,
truman_show: 4,
red_dawn: 1.5,
skyfall: 4,
alex_cross: 2,
uhf: 1,
lion_king: 3.5
}
# Add your code below!
movie_ratings.select{|k,v| v > 3.5 }.each_key{|k| puts k }
16
puts boolstr = ("123".is_a? String) ? true : false
17
"L".upto("P"){|n| puts n}
18
age.respond_to?(:next)
[1, 2, 3].respond_to?(:push)
19
alphabet = ["a", "b", "c"]
alphabet << "d" # Update me!
caption = "A giraffe surrounded by "
caption << "weezards!" # Me, too!
"<< the same as + or push"
20
puts "One is less than two!" if 1 < 2
puts 1 < 2 ? "One is less than two!" : "One is not less than two."
21
fibs = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
doubled_fibs = []
fibs.collect{|n| doubled_fibs<<n*2}
print doubled_fibs
22
my_nums = [1, 2, 3]
my_nums.collect { |num| num ** 2 }
# ==> [1, 4, 9]
23.0
def double(para)
yield para
end
double(2){|para| puts para*2}
23.1
def double(para)
yield para*2
end
double(2){|para| para}
24
multiples_of_3 = Proc.new do |n|
n % 3 == 0
end
(1..100).to_a.select(&multiples_of_3)
25
floats = [1.2, 3.45, 0.91, 7.727, 11.42, 482.911]
# Write your code below this line!
round_down = Proc.new do |n|
n = n.to_i
end
# Write your code above this line!
ints = floats.collect(&round_down)
"(The .collect! and .map! methods do the exact same thing.)"
26
group_1 = [4.1, 5.5, 3.2, 3.3, 6.1, 3.9, 4.7]
group_2 = [7.0, 3.8, 6.2, 6.1, 4.4, 4.9, 3.0]
group_3 = [5.5, 5.1, 3.9, 4.3, 4.9, 3.2, 3.2]
over_4_feet = Proc.new do |x|
x > 4
end
can_ride_1 = group_1.select(&over_4_feet)
27
def greeter()
yield
end
phrase = Proc.new do
puts "Hello there!"
end
greeter(&phrase)
hi = Proc.new do
puts "Hello!"
end
hi.call
28
numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
strings_array = numbers_array.map(&:to_s)
numbers_array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
strings_array = numbers_array.map(&:to_s).map(&:to_sym)
29
strings = ["leonardo", "donatello", "raphael", "michaelangelo"]
# Write your code below this line!
symbolize = lambda {|x| x = x.to_sym}
# Write your code above this line!
symbols = strings.collect(&symbolize)
30
lambda和Proc是一样的,除了Proc的return会跳出调用的方法,lambda则不会,它只是返回自己。
def foo
f = Proc.new { return "return from foo from inside proc" }
f.call # control leaves foo here
return "return from foo"
end
def bar
f = lambda { return "return from lambda" }
puts f.call # control does not leave bar here prints "return from lambda"
return "return from bar"
end
puts foo # prints "return from foo from inside proc"
puts bar # prints "return from bar"
31
my_array = ["raindrops", :kettles, "whiskers", :mittens, :packages]
# Add your code below!
symbol_filter = lambda { |x| x.is_a? Symbol }
symbols = my_array.select(&symbol_filter)
32
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 {|x,y| y.Upcase < "M"}
crew.each{|x,y| first_half}
33
class Creature
def initialize(name)
@name = name
end
end
class Person
def initialize(name)
@name = name
end
end
class Dragon < Creature; end
class Dragon < Person; end