Skip to content

Instantly share code, notes, and snippets.

@igaiga
igaiga / files_size.rb
Created July 5, 2011 07:55
Display all files size.
Dir.glob(**/*.*){ |f| puts "#{f} #{File.size(f)}" }
@igaiga
igaiga / gist:1224631
Created September 18, 2011 02:30
iOS sqlite select statement sample
//#import <sqlite3.h>
sqlite3* d = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent: @"testdb.db"];
if (sqlite3_open([path UTF8String], &d) != SQLITE_OK) {
// Even though the open failed, call close to properly clean up resources.
(defun quote-word()
(interactive)
(forward-char) ;; 単語先頭対策
(backward-word)
(insert "\"")
(forward-word)
(insert "\"")
)
module AcmeHttp
module Error; end
end
begin
begin
raise IOError, "Some IO error"
rescue Exception => error
error.extend(AcmeHttp::Error)
# class << error; p self.ancestors; end
@igaiga
igaiga / gist:1308716
Created October 24, 2011 10:12
Change UserAgent in NSMutableURLRequest
NSString *urlString = [NSString stringWithFormat:@"http://example.com/foo"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSString *userAgent = [NSString stringWithFormat:@"igaiga Browser"];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse *response;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
@igaiga
igaiga / gist:1308730
Created October 24, 2011 10:19
Set UIWebView's user agent into NSMutableURLRequest
NSString *urlString = [NSString stringWithFormat:@"http://example.com/foo"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// get User Agent in UIWebView
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString *userAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
NSLog(@"UserAgent: %@", userAgent);
[webView release];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
NSURLResponse *response;
@igaiga
igaiga / imgge_uploader.m
Created November 10, 2011 05:59
Multipart POST to upload jpeg sample code for iOS
@interface ImageUploader : NSObject {
NSData *theImage;
}
@property (retain) NSData *theImage;
- (void) syncUpload:(NSData *) uploadImage;
@end
#import "ImageUploader.h"
@igaiga
igaiga / gist:1475262
Created December 14, 2011 04:29
emacs_color_black.el
; color design for background black
;(add-to-list 'default-frame-alist '(alpha . (0.95 0.85)))
(set-background-color "black")
(set-foreground-color "white")
(custom-set-faces
'(font-lock-comment-face ((((class color) (min-colors 88) (background dark)) (:foreground "wheat1"))))
'(font-lock-function-name-face ((((class color) (min-colors 88) (background dark)) (:foreground "HotPink1" :weight bold))))
'(font-lock-keyword-face ((((class color) (min-colors 88) (background dark)) (:foreground "LightSalmon1"))))
'(font-lock-string-face ((((class color) (min-colors 88) (background dark)) (:foreground "bisque1"))))
'(font-lock-type-face ((((class color) (min-colors 88) (background dark)) (:foreground "DarkSeaGreen1" :weight bold))))
@igaiga
igaiga / sum.rb
Created May 24, 2012 13:53
Arrayオブジェクトの要素が奇数の項目を全て足すコードを書いてください。
# -*- coding: utf-8 -*-
# Arrayオブジェクトの要素が奇数の項目を全て足すコードを書いてください。
# 例えば array = [2,3,5,7,11] の場合、 3+5+7+11 = 26 が表示されればOKです。
# ヒント:奇数かどうか調べるのは Fixnum#odd? メソッド
# 1.odd? → true, 2.odd? → false
# ちなみに偶数か調べるのは Fixnum#even? メソッドです。
array = [2,3,5,7,11]
sum = 0
array.each do |i|
sum += i if i.odd?
@igaiga
igaiga / add_to_hash.rb
Created May 24, 2012 13:55
Arrayの中のHashの特定条件時にキーとバリューを加える
items = [{:title => "a", :price => 70},
{:title => "b", :price => 200},
{:title => "c", :price => 50}]
items.each do |item|
item[:special] = 'Low price!' if item[:price] < 100
end
#[{:title=>"a", :price=>70, :special=>"Low price!"},
# {:title=>"b", :price=>200},
# {:title=>"c", :price=>50, :special=>"Low price!"}]