Skip to content

Instantly share code, notes, and snippets.

@charissa
Created March 10, 2011 15:31
Show Gist options
  • Save charissa/864265 to your computer and use it in GitHub Desktop.
Save charissa/864265 to your computer and use it in GitHub Desktop.
Bicycle2 refactors Bicycle
#!/opt/local/bin/ruby1.9
include Math
class Bicycle
attr_accessor :wheel_size, :chainring, :cassette, :name
# Create the object
def initialize(name = "A Bicycle",wheel_size=27,chainring=42,cassette=[28,24,21,18,16,14,12,11])
@name = name
@wheel_size=wheel_size
@chainring=chainring
@cassette=cassette
@gear_i=0
end
# change gear
def set_gear(gear=1)
@gear_i=gear-1
end
# speed based on cadence
def mph(cadence)
# this may be imprecise/wrong so don't take my word for it....
# BTW 63360 is the number of inches in a mile
return cadence*Math::PI*@wheel_size*(@chainring/@cassette[@gear_i])*60/63360
end
def info
puts "Information for: #{@name}"
puts "This bicycle has the following characteristics:"
puts "Wheel size: #{@wheel_size} inches"
puts "Front gear has #{@chainring} teeth"
puts "Rear gears have #{@cassette} teeth"
puts "At a cadence of 100 rpm this bike can go approximately #{self.mph(100)} mph in gear #{1+@gear_i}"
end
end
if __FILE__ == $0
bike=Bicycle.new
bf_tikit=Bicycle.new("my bike",16.375,53,[28,24,21,18,16,14,12,11])
bike.set_gear(8)
bf_tikit.set_gear(8)
puts ""
# need to learn how to format to the console some day...
puts bf_tikit.mph(80)
puts bike.mph(80)
puts ""
puts bike.info
puts bf_tikit.info
end
#!/opt/local/bin/ruby1.9
include Math
# still has a bit of an odor...
class Bicycle
attr_accessor :wheel_size, :chainring, :cassette, :name
# Create the object
def initialize(arg={name: "A Bicycle",wheel_size: 27,chainring: 42,cassette: [28,24,21,18,16,14,12,11]})
@name = arg[:name]
@wheel_size=arg[:wheel_size]
@chainring=arg[:chainring]
@cassette=arg[:cassette]
@gear_i=0
end
# change gear
def set_gear(gear=1)
@gear_i=gear-1
end
# speed based on cadence
def mph(cadence)
# this may be imprecise/wrong so don't take my word for it....
# BTW 63360 is the number of inches in a mile
cadence*Math::PI*@wheel_size*(@chainring/@cassette[@gear_i])*60/63360
end
def info
puts %{
Information for: #{@name}
This bicycle has the following characteristics:
Wheel size: #{@wheel_size} inches
Front gear has #{@chainring} teeth
Rear gears have #{@cassette} teeth
}
# How horrible is this --- mixing puts-ing a string and printf! Gross.
printf("At cadence of 100 rpm this bike can go approximately %.1f mph in gear %2d", self.mph(100), 1+@gear_i)
end
end
if __FILE__ == $0
bike=Bicycle.new
bf_tikit=Bicycle.new({name: "my bike",wheel_size: 16.375,chainring: 53, cassette: [28,24,21,18,16,14,12,11]})
bike.set_gear(8)
bf_tikit.set_gear(8)
puts ""
# need to learn how to format to the console some day...
# You would think that someone would have devised a more intuitive way to format stuff by now.
# printf goes back to Fortan, COBOL and the stone ages...
#
printf("Bike Friday can go about %.1f mph at a cadence of 80.\n", bf_tikit.mph(80))
# and I am pretty sure that this is not Ruby-like... but I couldn't find any thing else for the time being...
printf("Regular bike can go about %.1f mph at a cadence of 80.\n", bike.mph(80))
puts ""
puts bike.info
puts bf_tikit.info
end
@charissa
Copy link
Author

OK I got it. sprintf returns a string. There is also a method called format that is synonymous with sprintf, if that looks better for readability...

 def info
%{
Information for: #{@name}
This bicycle has the following characteristics:
Wheel size: #{@wheel_size} inches
Front gear has #{@chainring} teeth
Rear gears have #{@cassette} teeth
#{sprintf("At cadence of 100 rpm this bike can go approximately %.1f mph in gear %2d", self.mph(100), 1+@gear_i)}
}
 end

or
#{format("At cadence of 100 rpm this bike can go approximately %.1f mph in gear %2d", self.mph(100), 1+@gear_i)}
for the last line of info

@snuggs
Copy link

snuggs commented Mar 16, 2011

FYI puts returns nil as well hence why it's an "anti-pattern" to call puts in a method ESPECIALLY as the last statement in a method. Why return nil if you get an implicit return for free? Usually you return nil ONLY when you explicitly want the return to be nil.

When it comes to formatting floats there is an easier way....

  1. you can call to_s on the float and now it's a string that can be formatted or...
  2. the % operator on strings.
    "%5.1f" % 345.6789 # "345.7"
    "%05d" % 123 # "00123"
    "%-5s" % "ID" # "ID "
    "%04x" % 0xfc14 # "fc14"
    "%6.2f, %05d" % [345.6789,123] # "345.68, 00123"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment