Skip to content

Instantly share code, notes, and snippets.

View sai43's full-sized avatar

Sai Ch sai43

View GitHub Profile
@sai43
sai43 / singletonexample.rb
Created October 14, 2014 04:04
singleton example in ruby
require 'singleton'
class Example
include Singleton
attr_accessor :keep, :strip
def _dump(depth)
# this strips the @strip information from the instance
Marshal.dump(@keep, depth)
end
@sai43
sai43 / nokogiriex.rb
Created October 14, 2014 04:53
Nokogiri gem example
require 'nokogiri'
require 'open-uri'
items = Array.new
prices = Array.new
doc = Nokogiri::HTML(open("http://www.thinkgeek.com/caffeine/feature/desc/0/all"))
items = doc.xpath("//div/a/h4").collect {|node| node.text.strip}
@sai43
sai43 / cwords.rb
Created October 15, 2014 05:11
analyze a text file, counting the occurrence of each word
# using fibers count words number of occurences in a file
words = Fiber.new do
File.foreach("samp.txt") do |line|
line.scan(/\w+/) do |word|
Fiber.yield word.downcase
end
end
nil
end
@sai43
sai43 / linkedlist.rb
Created October 16, 2014 06:16
sample linked list in ruby
class Node
attr_accessor :node, :next
def self.last(node)
return node if node.next.nil?
node = last node.next
end
def self.reverse(node)
return node if node.next.nil?
@sai43
sai43 / trainlenumber.java
Created October 16, 2014 06:33
Traingle number puzzle
package pack;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class TriangleNumbersPuzzle
{
private static int returnNumberOfDivisors(int inNum)
=begin
* Method should accept two parameters. First parameter is first number to display, second parameter should be number of rows to be printed.
* Any programming language is fine.
* Sample: printseries(7,4)
* Output should be:
* 7
* 14 15
* 28 29 30 31
* 56 57 58 59 60 61 62 63
=end
@sai43
sai43 / README.md
Last active August 29, 2015 14:11 — forked from Burgestrand/README.md

Magneto

It reads your torrents. Spit out magnet URIs.

Example Usage

$ ./magneto.rb magneto.rb.torrent

Results in:

@sai43
sai43 / server.rb
Created June 6, 2015 17:44
Simple Ruby client/server communication
require 'socket'
require 'win32/sound' #gem install win32-sound
require 'bigdecimal'
require 'bigdecimal/math'
include BigMath
include Win32
server = TCPServer.new("127.0.0.1",9090)
request = ""
bytest = ""
@sai43
sai43 / client.rb
Created June 6, 2015 17:47
Simple Ruby Client/Server Communication
require 'socket'
server = TCPSocket.open("127.0.0.1", 9090)
puts "You have the following options on this devilish server: \r"
puts "Type '%time' to see the time \r"
puts "Type '%hastalavista' to see the exit \r"
puts "Type '%pie' for some pie \r"
puts "Type '%why' to get some answer \r"
puts "Type '%Showtime' to get the image \r"
puts "Type '%nyan' to listen some music \r"
@sai43
sai43 / server.rb
Created June 7, 2015 14:39
Chat application in Ruby
#!/usr/bin/env ruby -w
require "socket"
class Server
def initialize( port, ip )
@server = TCPServer.open( ip, port )
@connections = Hash.new
@rooms = Hash.new
@clients = Hash.new
@connections[:server] = @server
@connections[:rooms] = @rooms