6. /ärt/
the expression or application of
human creative skill and
imagination, typically in a visual
form such as painting or
sculpture, producing works to be
appreciated primarily for their
beauty or emotional power.
7. /ärt/
the expression or application of
human creative skill and
imagination, typically in a visual
form such as painting or
sculpture, producing works to be
appreciated primarily for their
beauty or emotional power.
21. def play
puts "Welcome to the guessing game."
3.times do
guess = get_guess
if won? guess
@won = true
puts "You win!"
break
else
puts "Wrong!"
end
end
if !@won
puts "You lost. The actual number is #{@actual_number}."
end
end
22. def play
puts "Welcome to the guessing game."
3.times do
guess = get_guess
if won? guess
@won = true
puts "You win!"
break
else
puts "Wrong!"
end
end
if !@won
puts "You lost. The actual number is #{@actual_number}."
end
end
28. def play_rounds
@won = round_count.times.detect { play_round }
end
def play_round
if correct_guess? get_guess
true
else
puts "Wrong!"
end
end
29. def play_rounds
@won = round_count.times.detect { play_round }
end
def play_round
if correct_guess? get_guess
true
else
puts "Wrong!" # this returns nil which is falsey
end
end
36. def play
puts "Welcome to the guessing game."
3.times do
guess = get_guess
if won? guess
@won = true
puts "You win!"
break
else
puts "Wrong!"
end
end
if !@won
puts "You lost. The actual number is #{@actual_number}."
end
end
39. def play_rounds
@won = round_count.times.detect { play_round }
end
def play_round
if correct_guess? get_guess
true
else
puts "Wrong!"
end
end
40. def play_rounds
@won = round_count.times.detect { play_round }
end
def play_round
if correct_guess? get_guess
true
else
puts "Wrong!" # this is still bothering me
end
end
45. def play_round
if correct_guess? get_guess
correct_guess
else
incorrect_guess
end
end
def incorrect_guess
puts "Wrong!"
false
end
def correct_guess
puts "Correct"
false
end
46. def play_round
if correct_guess? get_guess
correct_guess
else
incorrect_guess
end
end
def incorrect_guess
puts "Wrong!"
false
end
def correct_guess
puts "Correct"
false
end
47. class Guess
def initialize actual_number
@actual_number = actual_number
@guess = get_guess
end
Extract
Class
Pattern
def status
if correct?
"Correct"
else
"Incorrect"
end
end
def correct?
guess == actual_number
end
private
attr_reader :guess, :actual_number
def get_guess
print "What is your guess: "
gets.to_i
end
end
54. If you’re willing to restrict the
flexibility of your approach, you can
almost always do something better.
John Carmack
55.
56.
57.
58. module Alchemist
module Conversion
def method_missing unit_name, *args, &block
exponent, unit_name = Alchemist.parse_prefix(unit_name)
if Alchemist.has_measurement?(unit_name)
Alchemist.measurement self, unit_name, exponent
else
super( unit_name, *args, &block )
end
end
end
end
class Numeric
include Alchemist::Conversion
end
59. module Alchemist
module Conversion
def method_missing unit_name, *args, &block
exponent, unit_name = Alchemist.parse_prefix(unit_name)
if Alchemist.has_measurement?(unit_name)
Alchemist.measurement self, unit_name, exponent
else
super( unit_name, *args, &block )
end
end
end
end
class Numeric
include Alchemist::Conversion
end
60. def self.parse_prefix(unit)
matches = unit.to_s.match(prefix_matcher)
prefix, parsed_unit = matches.captures
if prefix && si_units.include?(parsed_unit)
value = prefixed_value_for(prefix.to_sym, parsed_unit)
[value, parsed_unit.to_sym]
else
[1, unit]
end
end
61. def self.parse_prefix(unit)
matches = unit.to_s.match(prefix_matcher)
prefix, parsed_unit = matches.captures
if prefix && si_units.include?(parsed_unit)
value = prefixed_value_for(prefix.to_sym, parsed_unit)
[value, parsed_unit.to_sym]
else
[1, unit]
end
end
62. def self.parse_prefix(unit)
matches = unit.to_s.match(prefix_matcher)
prefix, parsed_unit = matches.captures
if prefix && si_units.include?(parsed_unit)
value = prefixed_value_for(prefix.to_sym, parsed_unit)
[value, parsed_unit.to_sym]
else
[1, unit]
end
end
77. module Alchemist
def self.setup category = nil
if category
load_category category
else
load_all_categories
end
end
private
def self.load_all_categories
library.load_all_categories
end
def self.load_category category
library.load_category category
end
end
78. module Alchemist
class ModuleBuilder
def initialize category
@category = category
end
def build
Module.new.tap do |category_module|
category_module.class_eval %(def self.inspect()
"#<Module(#{category})>" end)
category_module.class_eval category_methods
end
end
79. private
attr_reader :category
def library
Alchemist.library
end
def category_methods
unit_names.map do |name|
%(define_method("#{name}") { Alchemist.measure self, :#{name} }) +
"n" + prefixed_methods(name)
end.join("n")
end
def unit_names
library.unit_names(category)
end
def prefixes_with_value(name)
if library.si_units.include?(name.to_s)
library.unit_prefixes
else
[]
end
end
def prefixed_methods(name)
prefixes_with_value(name).map do |prefix, value|
%(define_method("#{prefix}#{name}") { Alchemist.measure self, :#{name}, #{value} })
end.join("n")
end
80. module Alchemist
class ModuleBuilder
def initialize category
@category = category
end
def build
Module.new.tap do |category_module|
category_module.class_eval %(def self.inspect()
"#<Module(#{category})>" end)
category_module.class_eval category_methods
end
end
81. module Alchemist
class ModuleBuilder
def initialize category
@category = category
end
def build
build_module do |category_module|
define_inspect_method(category_module)
define_unit_methods(category_module)
end
end