Skip to content

Instantly share code, notes, and snippets.

@jewel12
jewel12 / class_included.rb
Created July 1, 2012 18:03
Module.included
module M
def method; "method" end
def self.included(klass)
klass.extend ClassMethods
end
module ClassMethods
def c_method; "c_method" end
end
@jewel12
jewel12 / g_search_24h.pl
Created June 16, 2012 06:09
24時間以内の検索結果を取得
#!/usr/bin/env perl
use strict;
use warnings;
use WWW::Mechanize;
use HTML::TreeBuilder::XPath;
use YAML;
use Encode;
use utf8;
@jewel12
jewel12 / gist:2873112
Created June 5, 2012 06:30
move-to-next-blank-line
(defun blank-line? ()
(string-match "^\n$" (substring-no-properties (thing-at-point 'line))))
(defun move-to-next-blank-line ()
(interactive)
(progn (forward-line 1)
(if (blank-line?) () (move-to-next-blank-line))))
(defun move-to-previous-blank-line ()
(interactive)
@jewel12
jewel12 / enumerable_cycle.rb
Created January 11, 2012 15:17
Enumerable#cycle
arr = (1..3).to_a.cycle
arr.next # => 1
arr.next # => 2
arr.next # => 3
arr.next # => 1
arr.next # => 2
arr.next # => 3 以下繰り返し
a = ('a'..'z').to_a
# coding:utf-8
class NinjaGreeting
def initialize
@ninja = {}
end
def greet( name )
puts @ninja[ name ] ||= begin
warn( 'First call' )
@jewel12
jewel12 / Rakefile_multitask.rb
Created December 6, 2011 05:24
Rake : タスクを動的に生成して並列に実行
params = (0..5).step(1)
multitask :deploy => [*params.map{|i| "task_#{i}"}] do
puts "Complete!"
end
params.each do |param|
task "task_#{param}" do |task|
puts "-- #{task.name} : #{param} --"
end
@jewel12
jewel12 / Rakefile_call_other_task.rb
Created December 5, 2011 19:17
Rake で Task 内で別の Task を呼ぶ
desc "Repeater"
task :repeat do
3.times do |i|
Rake::Task[:t].execute(i)
puts '---'
end
end
desc "Small task"
task :t, :arg do |task, arg|
@jewel12
jewel12 / elem_check.rb
Created November 22, 2011 18:45
要素の存在をチェックするための Hash
words = open( stop_words_file ).readlines.map(&:chomp)
# 2行使うが分かりやすい
stop_words = {}
words.each {|word| stop_words[word] = true }
stop_words = Hash[ *words.zip([true]*words.size) ] # 醜い?
stop_words = Hash[ *words.map{|word| [word, true]} ] # 少し遅い?
@jewel12
jewel12 / kanji_stream.rb
Created October 9, 2011 17:04
TwitterStream sample から漢字を抽出
# coding:utf-8
require 'tweetstream'
TweetStream.configure do |config|
config.consumer_key = CONSUMER_KEY
config.consumer_secret = CONSUMER_SECRET
config.oauth_token = OAUTH_TOKEN
config.oauth_token_secret = OAUTH_SECRET
config.auth_method = :oauth
config.parser = :yajl
@jewel12
jewel12 / dwm_dialogue.rb
Created July 10, 2011 18:58
Dialogue 1800 の暗記スケジュールを作る
# coding:utf-8
require 'date'
class AnkiData
include Enumerable
def initialize( &block )
@anki_set_maker = block
@data = Array.new
end