# 1
puts 4 * 10
puts 5 - 12
puts 30 / 4
"Jimmy"
"Jimmy".reverse
"Jimmy".length
"Jimmy" * 5
Ruby
복사
# 2
40.to_s.reverse
[]
[12, 47, 35]
[12, 47, 35].max
ticket = [12, 47, 35]
ticket
ticket.sort!
puts ticket[0]
puts ticket[1]
puts ticket[2]
Ruby
복사
# 3
poem.gsub("toast", "honeydew")
poem.reverse
poem.lines.reverse
puts poem.lines.reverse.join
Ruby
복사
# 4
books["Gravitys Rainbow"] = :splendid
books["The deep end"] = :abysmal
books["Living colors"] = :mediocre
puts books
puts books.length
puts books["Gravitys Rainbow"]
books.keys
ratings = Hash.new {0}
books.values.each { |rate|
ratings[rate] += 1
}
puts ratings
5.times { print "Odelay! " }
5.times { |time|
puts time
}
Ruby
복사
# 5
puts "Hello"
puts("Hello")
def tame( number_of_shrews )
end
def tame( number_of_shrews )
number_of_shrews.times {
puts "Tamed a shrew"
}
end
tame 5
def tame( number_of_shrews )
number_of_shrews.times {
puts "Tamed a shrew"
}
return number_of_shrews
end
puts tame(3)
Ruby
복사
# 6
#
get_shakey
#
s = get_shakey
s["William Shakespeare"].each { |key, val|
puts val["title"]
}
#
def count_plays(year)
s = get_shakey
s["William Shakespeare"]
.select { |k, v|
v["finished"] == year
}.each { |key, val|
puts val["title"]
}.count
end
puts count_plays(1591)
#
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
print_plays(1600, 1605)
#
if 1 < 2
puts "It is true: 1 is less than 2"
end
#
a = 0
if a == 100
puts "Expression is true, but a is now: #{a}"
else
puts "#{a} is not equal to 100"
end
#
def hungry?(time_of_day_in_hours)
if time_of_day_in_hours >= 12
puts "Me hungry"
true
else
puts "Me not hungry"
false
end
end
def eat_an(what)
puts "Me eat #{what}\n"
end
eat_an 'apple' if hungry?(14)
eat_an 'apple' if hungry?(10)
#
Ruby
복사
# 7
class Blurb
end
#
class Blurb
attr_accessor :content, :time, :mood
end
#
blurb1 = Blurb.new
puts blurb1
blurb1.content = "Today Mount Hood Was Stolen!"
#
blurb1.time = Time.now
blurb1.mood = :sick
puts blurb1
#
class Blurb
attr_accessor :content, :time, :mood
def initialize(mood, content="")
@time = Time.now
@content = content[0..39]
@mood = mood
end
end
Blurb.new(:sick).time
#
Blurb2 = Blurb.new :confused, "I can not believe Mt. Hood was stolen!"
Ruby
복사
# 8
class Blurbalizer
def initialize(title)
@title = title
@blurbs = [] # A fresh clean array
# for storing Blurbs
end
def add_a_blurb(mood, content)
# The << means add to the end of the array
@blurbs << Blurb.new(mood, content)
end
def show_timeline
puts "Blurbify: #{@title} has #{@blurbs.count} Blurbs"
@blurbs.sort_by { |t|
t.time
}.reverse.each { |t|
puts "#{t.content.ljust(40)} #{t.time}"
}
end
end
myapp = Blurbalizer.new "The Big Blurb"
#
myapp.add_a_blurb :moody, "Add Blurb here"
myapp.show_timeline
#
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
Ruby
복사