#substitute string_name for your variable or a string
#substitute "-" for your choice of character
#note: doesn't recognize accented characters properly
string_name.gsub(/[^a-z0-9]+/i, '-')
class Float
# USAGE : 123123123.123131.decimal # => 123123123.12
# OR 123123123.123131.decimal 4 # => 123123123.1231
# Defaults to a scale of 2
def decimal(prec=2)
number = self.to_s.split(".")[0]
scale = self.to_s.split(".")[1][0..(prec-1)]
"#{number}.#{scale}".to_f
end
end
Infinity = 1.0/0
# a rather useless range
everything = -Infinity..Infinity
puts everything.includes?(5) #-> true
# use it for representing an unbounded value
storage_limits => { :demo => 0,
:standard => 250.megabytes,
:expert => 1.gigabyte,
:unlimited => Infinity }
if bytes_used < storage_limits[account_level]
# add another file or something
else
# display "out of space" message
end
def add_to_cart
begin
product = Product.find(params[:id])
# if can not find product id, catch exception, raise error to be
# displayed on 'index' page
rescue ActiveRecord::RecordNotFound
logger.error("Atempt to access invalid product #{params[:id]}")
flash[:notice] = "invalid product"
redirect_to :action => :index
else
# if can find product id, continue to add product to cart
@cart = find_cart
@cart.add_product(product)
end
end
# RHTML code
<% if flash[:notice] -%>
<div id="notice"><%= flash[:notice] %></div>
<% end -%>