Skip to content

Instantly share code, notes, and snippets.

@cieux1
cieux1 / vender.rb
Created March 7, 2012 12:38
ruby class study 01
#! /usr/bin/local/ruby
# -*- encoding: utf-8 -*-
class Vender
@@customer = 0
def initialize(location="那覇", drink="Fantaウコン")
@location = location
@drink = drink
@price = rand(9999)
@cieux1
cieux1 / find-matched-html.rb
Created March 19, 2012 07:41
find matched htmls
#! /usr/bin/local/ruby
# -*- encoding: utf-8 -*-
pattern = 'foo'
puts "pattern to match: " + pattern
count = 0
match_count = 0
error_count = 0
@cieux1
cieux1 / repalce-matched-html.rb
Created March 19, 2012 08:05
replace matched pattern in htmls
#! /usr/bin/local/ruby
# -*- encoding: utf-8 -*-
pattern = 'foo'
new_pattern = 'bar'
puts "pattern to be replaced: " + pattern
puts "new pattern: " + new_pattern
count = 0
replace_count = 0
@cieux1
cieux1 / ga-click-counts.js
Created March 27, 2012 01:08
google analytics count clicks
jQuery(function($) {
var _thisUrl = $(location).attr('href');
$.each($('a,area,input:image,input:submit'), function() {
var _thisId = $(this).closest("[id]").attr("id");
$(this).click(function() {
_gaq.push(['_trackEvent', _thisUrl, _thisId, $(this).attr('href')]);
});
});
});
@cieux1
cieux1 / cv_rate_calculate.rb
Created March 29, 2012 08:17
bulk calculate the conversion rate of EACH PAGEs by combining 2 csv files extracted from google analytics.
require "csv"
#to begin with... save visits number as pv.csv, conversion number as cv.csv.
def make_hash(file)
h = {}
s = CSV.open(file, "r")
s.each do |row|
# use row[1] for pageviews, row[2] for visits
h[row[0]] = row[2].delete(",") if row[0] =~ /YOUR\.SITE\.COM\//
@cieux1
cieux1 / meta-info-extract.rb
Created April 3, 2012 07:23
extract title, meta info, keywords and header text to csv files, from locally saved sites.
# makes 2 csv files. kw has keywords. ko has no keywords but much easier to filter words and find duplicate texts.
# works only utf8 htmls.
# needs lots of refactoring...
require 'rubygems'
require 'hpricot'
require 'open-uri'
@cieux1
cieux1 / bar-not-foobar.rb
Created April 4, 2012 11:56
bar not foobar
words = ["foobar", "bar"]
words.each do |w|
if w !~ /foobar/ && w =~ /bar/
puts w
end
end
@cieux1
cieux1 / find-and-output.rb
Created April 4, 2012 12:16
ruby study - open file, match with a pattern and output result to a file
contents = File.read(ARGV[0])
matched = []
matched << contents.scan(/puts "(.*?)"/)
puts matched
# % ruby find-and-output.rb hogehoge.rb > output.txt
@cieux1
cieux1 / count_prime_num.rb
Created May 2, 2012 14:36
count prime numbers
#! /usr/bin/local/ruby
# -*- encoding: utf-8 -*-
def prime?(num)
2.upto(num-1) do |x|
if num % x == 0
true
break
end
end
@cieux1
cieux1 / ex-11.rb
Created May 9, 2012 13:30
ruby exercise 11-1
#! /usr/bin/local/ruby
# -*- encoding: utf-8 -*-
#1
a = Array.new(100) {|x| x+1}
#2
a2 = a.map {|x| x*100}
a.map! {|x| x*100} # destructive