Bookmark App In Ruby
Here’s a little app I wrote using Ruby and Sinatra to keep track of links that I want to come back and read.
It displays the newest links at the top of the page kind of like a stack.
Code is a little messy, but it works.
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
get '/' do
page = <<EOHTML
<html>
<head>
<title>bookmarks</title>
</head>
<body>
<p><a href="/add">add new bookmark</a></p>
#{htmlize}
<h6>powered by Sinatra</h6>
</body>
</html>
EOHTML
page
end
get '/remove/:id' do
remove(params[:id])
page = <<EOT
<html>
<head>
<title>bookmarks</title>
</head>
<body>
<p><a href="/add">add new bookmark</a></p>
<h2>Bookmark Removed</h2>
#{htmlize}
</body>
</html>
EOT
end
get '/add' do
page = <<EOT
<html>
<head>
<title>bookmarks - add</title>
</head>
<body>
<form action="/add" method="post">
<input type="text" name="link">link<br>
<input type="text" name="topic">topic<br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
EOT
end
post '/add' do
add(params[:topic], params[:link])
page = <<EOT
<html>
<head>
<title>bookmarks</title>
</head>
<body>
<p><a href="/add">add new bookmark</a></p>
<h2>Bookmark Added</h2>
#{htmlize}
</body
</html>
EOT
end
def add(topic, link)
outfile = open("bm.data", "a")
outfile.puts "#{topic}\t#{link}"
outfile.close
end
def show
infile = open("bm.data", "r")
infile.each { |line| puts line }
infile.close
end
def list
infile = open("bm.data", "r")
contents = infile.readlines
infile.close
contents
end
def htmlize
output = ''
lines = list
id = lines.length + 1
output << "<table>\n"
lines.reverse.each do |line|
id = id - 1
output << "<tr>\n"
(topic, link) = line.chomp.split(/\t/)
output +=<<EOT
<td><a href="/remove/#{id}">[X]</a></td>
<td>#{topic}</td>
<td><a href="#{link}">#{link}</a></td>
EOT
output << "</tr>\n"
end
output << "</table>\n"
output
end
def remove(id)
infile = open("bm.data", "r")
outfile = open("bm.data.new", "w")
count = 0
infile.each do |line|
count = count + 1
outfile.puts line unless id.to_s == count.to_s
end
infile.close
outfile.close
File.rename("bm.data.new", "bm.data")
end
arbitrary precision arithmetic in c and c++
here are the packages i have found easiest to deal with for doing arbitrary precision integer arithmetic in c and c++. i have used ttmath with c++ the most. it’s not technically an arbitrary arithmetic library since you have to declare the bit size of the integer you want to support. but you can make that bit size as large as you want.
for c it seems like the bn library that is included in openssh is the easiest to work with. i’ve looked at gmp a bit, but it’s a lot of complexity if you don’t need it’s features.
current books
i seem to be working my way through learn ruby the hard way at the moment. so i’m actually reading three books right now.
- unix shell programming
- simply scheme
- learn ruby the hard way