times
3.times do
print 'Welcome '
end
# Welcome Welcome Welcome
2 + 6
2 + 6
4 * 10
5 - 12
30 / 4
"Jimmy"
# 7.5
puts
puts 4 * 10
puts 5 - 12
puts 30 / 4
string
"Jimmy".reverse
# ymmiJ
"Jimmy".length
# 5
"Jimmy" * 5
# JimmyJimmyJimmyJimmyJimmy
40.to_s.reverse
# 04
poem.gsub("toast", "honeydew")
poem.reverse
poem.lines.reverse
puts poem.lines.reverse.join
array
[12, 47, 35]
[12, 47, 35].max
# 47
ticket = [12, 47, 35]
# [12, 47, 35]
ticket
# [12, 47, 35]
ticket.sort!
# [12, 35, 47]
books = {}
#{}
books["Gravitys Rainbow"] = :splendid
#splendid
books["The deep end"] = :abysmal
books["Living colors"] = :mediocre
puts books
#{"Gravitys Rainbow"=>"splendid", "The deep end"=>"abysmal", "Living colors"=>"mediocre"}
puts books.length
#3
puts books["Gravitys Rainbow"]
#splendid
books.keys
#["Gravitys Rainbow", "The deep end", "Living colors"]
ratings = Hash.new {0}
books.values.each { |rate|
ratings[rate] += 1
}
puts ratings
#{"splendid"=>1, "abysmal"=>1, "mediocre"=>2}
5.times { print "Odelay! " }
#Odelay! Odelay! Odelay! Odelay! Odelay!
2.times { |time|
puts time
}
#0
#1
方法括号
puts "Hello"
puts("Hello")
def
def tame( number_of_shrews )
end
def tame( number_of_shrews )
number_of_shrews.times {
puts "Tamed a shrew"
}
end
tame 2
# Tamed a shrew
# Tamed a shrew
def tame( number_of_shrews )
number_of_shrews.times {
puts "Tamed a shrew"
}
return number_of_shrews
end
puts tame(3)
迭代
def print_plays(year_from, year_to)
get_shakey["William Shakespeare"]
.select { |k, v|
year_from <= v["finished"] &&
year_to >= v["finished"]
}.each { |k, v|
puts "#{v["title"].ljust(30)} #{v["finished"]}"
}
end
get_shakey = {"William Shakespeare"=>{"1"=>{"title"=>"The Two Gentlemen of Verona", "finished"=>1591}, "2"=>{"title"=>"The Taming of the Shrew", "finished"=>1591}, "3"=>{"title"=>"Henry VI, Part 2", "finished"=>1591}, "4"=>{"title"=>"Henry VI, Part 3", "finished"=>1591}, "5"=>{"title"=>"Henry VI, Part 1", "finished"=>1592}, "6"=>{"title"=>"Titus Andronicus", "finished"=>1592}, "7"=>{"title"=>"Richard III", "finished"=>1593}, "8"=>{"title"=>"Edward III", "finished"=>1593}, "9"=>{"title"=>"The Comedy of Errors", "finished"=>1594}, "10"=>{"title"=>"Love's Labour's Lost", "finished"=>1595}, "11"=>{"title"=>"Love's Labour's Won", "finished"=>1596}, "12"=>{"title"=>"Richard II", "finished"=>1595}, "13"=>{"title"=>"Romeo and Juliet", "finished"=>1595}, "14"=>{"title"=>"A Midsummer Night's Dream", "finished"=>1595}, "15"=>{"title"=>"King John", "finished"=>1596}, "16"=>{"title"=>"The Merchant of Venice", "finished"=>1597}, "17"=>{"title"=>"Henry IV, Part 1", "finished"=>1597}, "18"=>{"title"=>"The Merry Wives of Windsor", "finished"=>1597}, "19"=>{"title"=>"Henry IV, Part 2", "finished"=>1598}, "20"=>{"title"=>"Much Ado About Nothing", "finished"=>1599}, "21"=>{"title"=>"Henry V", "finished"=>1599}, "22"=>{"title"=>"Julius Caesar", "finished"=>1599}, "23"=>{"title"=>"As You Like It", "finished"=>1600}, "24"=>{"title"=>"Hamlet", "finished"=>1601}, "25"=>{"title"=>"Twelfth Night", "finished"=>1601}, "26"=>{"title"=>"Troilus and Cressida", "finished"=>1602}, "27"=>{"title"=>"Sir Thomas More", "finished"=>1604}, "28"=>{"title"=>"Measure for Measure", "finished"=>1604}, "29"=>{"title"=>"Othello", "finished"=>1604}, "30"=>{"title"=>"All's Well That Ends Well", "finished"=>1605}, "31"=>{"title"=>"King Lear", "finished"=>1606}, "32"=>{"title"=>"Timon of Athens", "finished"=>1606}, "33"=>{"title"=>"Macbeth", "finished"=>1606}, "34"=>{"title"=>"Antony and Cleopatra", "finished"=>1606}, "35"=>{"title"=>"Pericles, Prince of Tyre", "finished"=>1608}, "36"=>{"title"=>"Coriolanus", "finished"=>1608}, "37"=>{"title"=>"The Winter's Tale", "finished"=>1611}, "38"=>{"title"=>"Cymbeline", "finished"=>1610}, "39"=>{"title"=>"The Tempest", "finished"=>1611}, "40"=>{"title"=>"Cardenio", "finished"=>1613}, "41"=>{"title"=>"Henry VIII", "finished"=>1613}, "42"=>{"title"=>"The Two Noble Kinsmen", "finished"=>1614}}}
print_plays(1600, 1605)
if
https://ruby-doc.org/core-3.1.2/doc/syntax/control_expressions_rdoc.html
if 1 < 2
puts "It is true: 1 is less than 2"
end
a = 10
if a == 100
puts "Expression is true, but a is now: #{a}"
elsif a == 10
puts "Expression is true, but a is now: #{a}"
else
puts "#{a} is not equal to 100"
end
class
https://ruby-doc.org/core-3.1.2/doc/syntax/modules_and_classes_rdoc.html
class Blurb
attr_accessor :content, :time, :mood
def initialize(mood, content="")
@time = Time.now
@content = content[0..39]
@mood = mood
end
end
blurb1 = Blurb.new
puts blurb1
blurb1.content = "Today Mount Hood Was Stolen!"
class Blurb
attr_accessor :content, :time, :mood
def initialize(mood, content="")
@time = Time.now
@content = content[0..39]
@mood = mood
end
def moodify
if @mood == :sad
return ":-("
elsif @mood == :happy
return ":-)"
# Add other moods here
end
# The default mood
":-|"
end
end
class Blurbalizer
def initialize(title)
@title = title
@blurbs = []
end
def add_a_blurb(mood, content)
@blurbs << Blurb.new(mood, content)
end
def show_timeline
puts "Blurbalizer: #{@title} has #{@blurbs.count} Blurbs"
@blurbs.sort_by { |t|
t.time
}.reverse.each { |t|
puts "#{t.content.ljust(40)} #{t.time}"
}
end
end
myapp.show_timeline
结