Skip to content

Commit 1fb45a9

Browse files
4 little scripts added
0 parents  commit 1fb45a9

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

README

Whitespace-only changes.

age_in_seconds.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/ruby
2+
3+
def myage (seconds, measures)
4+
today = Time.now
5+
puts seconds
6+
birth = today - seconds
7+
retval = "You are %s old"
8+
string = ''
9+
measures.each do |measure|
10+
string += "%d #{measure}" % eval("today." + measure) - eval("birth." + measure)
11+
end
12+
puts retval % string
13+
end
14+
15+
puts myage(979600, ['year', 'month', 'day', 'hour', 'minute'])

euler1.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/ruby
2+
solution = 0
3+
(0..1000).each do |n|
4+
if n % 3 == 0 || n % 5 == 0
5+
solution += n
6+
end
7+
end
8+
puts solution

euler2.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/ruby
2+
def fibonacci n, fib1=1, fib2=0
3+
oldfib = fib1
4+
fib = fib1 + fib2
5+
fib2 = oldfib
6+
fib1 = fib
7+
8+
if n > 0
9+
fib = fibonacci(n-1, fib1, fib2)
10+
end
11+
fib
12+
end
13+
14+
n = 1
15+
evenfibs = 0
16+
while (fib = fibonacci(n)) <= 4000000
17+
if fib % 2 == 0
18+
evenfibs += fib
19+
end
20+
n += 1
21+
end
22+
puts evenfibs

twit.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/ruby
2+
require 'rubygems'
3+
require 'twitter'
4+
require 'optparse'
5+
6+
# UI Application
7+
8+
$options = {}
9+
10+
class App
11+
def initialize(args)
12+
begin
13+
parse_options(args)
14+
rescue ArgumentError, OptionParser::ParseError => msg
15+
$stderr.print "Error: #{msg}\n"
16+
exit
17+
end
18+
end
19+
20+
def parse_options(args)
21+
commands = [:update, :user_timeline, :list_friends, :list_followers, :make_friend]
22+
23+
OptionParser.new do |opts|
24+
opts.banner = "Usage: twitter [options]"
25+
26+
opts.on("-c", "--command COMMAND", commands) do |v|
27+
$options[:command] = v
28+
end
29+
30+
opts.on("-m", "--message MESSAGE") do |v|
31+
$options[:message] = v
32+
end
33+
34+
opts.on("-u", "--username USER") do |v|
35+
$options[:username] = v
36+
end
37+
38+
opts.on("-p", "--password PASSWORD") do |v|
39+
$options[:password] = v
40+
end
41+
42+
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
43+
$options[:verbose] = v
44+
end
45+
46+
opts.on("-i", "--id", "User Id") do |v|
47+
$options[:userid] = v
48+
end
49+
50+
opts.on_tail("-h", "--help", "Show this message") do
51+
puts opts
52+
exit
53+
end
54+
end.parse!(args)
55+
56+
case $options[:command]
57+
when :update
58+
raise ArgumentError, "Update needs a message" if not $options[:message]
59+
when :make_friend
60+
raise ArgumentError, "Make Friend needs a user id" if not $options[:userid]
61+
end
62+
end
63+
64+
def run()
65+
auth = Twitter::HTTPAuth.new($options[:username], $options[:password])
66+
twitter = Twitter::Base.new(auth)
67+
68+
case $options[:command]
69+
when :update
70+
print "update: %s\n" % [$options[:message]] if $options[:verbose]
71+
twitter.update($options[:message])
72+
when :user_timeline
73+
twitter.user_timeline.each do |mash|
74+
mash.each do |var|
75+
76+
end
77+
end
78+
when :list_followers
79+
puts twitter.follower_ids
80+
when :list_friends
81+
puts twitter.friend_ids
82+
when :make_friend
83+
twitter.friendship_create($options[:userid])
84+
end
85+
end
86+
end
87+
88+
app = App.new(ARGV)
89+
app.run()

0 commit comments

Comments
 (0)