info

Home
[Ruby]ファイルを読み込む  @ Ruby 2014年9月3日
ファイルを読み込む

サンプル
#!/usr/bin/ruby-1.9.3
# coding: utf-8
require 'json'

puts "Content-type: application/json\n\n"

filename="test.txt"
results=Array.new

if File.exist?(filename)
    f = open(filename)
    f.each{|line|
        results.push(line.force_encoding("utf-8"))
    }
    f.close
else
    results.push("no data")
end

puts JSON.generate({'time'=>Time.now.strftime("%Y-%m-%d %H:%M:%S"), 'data'=>results})
[Ruby]MySQLからデータ取得  @ MySQL , Ruby 2014年9月2日
MySQLからデータ取得

サンプル
#!/usr/bin/ruby-1.9.3
# coding: utf-8
require 'json'
require 'mysql'

puts "Content-type: application/json\n\n"

connection = Mysql.connect('host','user','password','db name')
connection.query("set character set utf8") #文字化けするとき

columns=["ID","post_title","post_date"]
result = connection.query("SELECT "+columns.join(",")+" FROM wp_posts WHERE post_status='publish' ORDER BY ID DESC LIMIT 5")
connection.close

results=Array.new
result.each {|record|
    w = {}
    record.each_with_index{|val,i|
        key = columns[i]
        w[key.force_encoding("utf-8")]=val.force_encoding("utf-8") #文字化けするときforce_encoding
    }
    results.push(w)
}
puts JSON.generate({'time'=>Time.now.strftime("%Y-%m-%d %H:%M:%S"), 'data'=>results})
[MySQL]MySQLの文字コードの設定を確認する  @ MySQL 2014年9月1日
MySQLの文字コードの設定を確認する

クエリ
SHOW VARIABLES LIKE 'character_set%'

結果
Variable_name Value
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_filesystem binary
character_set_results utf8
character_set_server utf8
character_set_system utf8
character_sets_dir /usr/share/mysql/charsets/
[Ruby]jsonを出力する  @ Ruby 2014年8月29日
jsonを出力する

サンプル
#coding:utf-8
require 'json'
print("Content-type: application/json\n\n")

print JSON.generate({"time"=>Time.now.strftime("%Y-%m-%d %H:%M:%S"),
                    "user"=>[
                                {"id" => 32, "name"=>"jeff"},
                                {"id" => 56, "name"=>"john"}
                            ]
                    })
[Swift]Swift #5 animateWithDuration  @ App Develop , Swift 2014年7月30日
Swift #5 animateWithDuration
@IBOutlet weak var mainLabel: UILabel!

func initUI(){
    var message:String = getMessage(message:"いあうえ",message2:"390u8")
    println(message)
        
    message = getMessage(message2:"むぎちゃ")
    println(message)

    UIView.animateWithDuration(
        2.0,
        delay: 1.0,
        usingSpringWithDamping: 1.0,
        initialSpringVelocity: 0.0,
        options: UIViewAnimationOptions.CurveEaseInOut,
        animations:{()in
            self.mainLabel.frame = CGRectMake(100.0, 300.0, self.mainLabel.frame.size.width, self.mainLabel.frame.size.height)
        },
        completion:{(finished)in
            println("finished:\(finished)")
            
        })
}
func getMessage(var message:String="ABC",var message2:String="MBNRKF")->String{
    return "\(message) \(message2)"
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果
いあうえ 390u8
ABC むぎちゃ
finished:true
[Swift]Swift #4 カメラを扱う  @ App Develop , Swift 2014年7月29日
Swift #4 カメラを扱う

Main.storyboard
Main.storyboard

ViewController
class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
    
    @IBOutlet var mainImageView: UIImageView
    var picker:UIImagePickerController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        picker = UIImagePickerController()
        picker!.delegate = self
        picker!.allowsEditing = false
    }

    @IBAction func onTapButton1(button:UIButton){
        println( "onTapButton - open album" )
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
            picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            self.presentViewController(picker, animated: true, completion: nil)
        }
    }
    
    @IBAction func onTapButton2(button:UIButton){
        println( "onTapButton - take photo" )
        
        if picker==nil {
            return
        }
        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
            picker!.sourceType = UIImagePickerControllerSourceType.Camera
            self.presentViewController(picker, animated: true, completion: nil)
        }
    }
    
    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
        println( "imagePickerController" )
        self.dismissViewControllerAnimated(true, completion: nil)
        mainImageView.image = image
        if picker!.sourceType == UIImagePickerControllerSourceType.Camera {
            println( "save photo" )
            UIImageWriteToSavedPhotosAlbum(image, self, "onSaveImageWithUIImage:error:contextInfo:", nil)
        }
    }
    
    func onSaveImageWithUIImage(image: UIImage!, error: NSErrorPointer, contextInfo: UnsafePointer<()>){
        if error {
            println( "error" )
            return
        }
        println( "success" )
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController!){
        println( "imagePickerControllerDidCancel" )
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
[Swift]Swift #3  @ App Develop , Swift 2014年7月28日
Swift #3
func initUI(){
    //配列-固定
    println( "-------------- array1" )
    let array1 = [0, 3, "あいうえお"]
    println( array1 )
    
    //配列-可変
    println( "-------------- array2" )
    var array2:[Int] = [1,2,3]
    array2[1]=57
    println( array2 )
    
    println( "-------------- array3" )
    var array3:[Float] = [4.0, 768.0, 0.432, 0.2]
    array3[0]=5.0
    array3[2]=346.0
    println( array3 )
    
    println( "-------------- array4" )
    var array4:[String] = ["か","き","く","け"]
    array4[2]="さしすせそ"
    println( array4 )
    
    //Dictionary
    println( "-------------- Dictionary" )
    var dic1:[String:NSObject?] = ["name":"た","address":"ち","number":"つ"];
    println( dic1 )
    println( dic1["address"] )
    dic1["address"]="マダガスカル"
    println( dic1 )
    
    //for in 1
    println( "-------------- for in 1" )
    for value in array2 {
        println( value )
    }
    
    //for in 2
    println( "-------------- for in 2" )
    for obj in dic1 {
        println( obj )
    }
    
    //for in 3
    println( "-------------- for in 3" )
    for i in 1..<8 {
        println( i )
    }
    
    //for in 4
    println( "-------------- for in 4" )
    for i in 1...8 {
        println( i )
    }
    
    //for in 5
    println( "-------------- for in 5" )
    for i in 0..<array3.count {
        println( "\(i) : \(array3[i])" )
    }
    
    //for in 6
    println( "-------------- for in 6" )
    for obj in dic1 {
        var (key,value)=obj
        println( "\(key) : \(value)" )
    }
    
    //for
    println( "-------------- for" )
    for var i=0;i<10;i++ {
        println( "for : \(i)" )
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    initUI()
}

出力結果
-------------- array1
(
    0,
    3,
    "\U3042\U3044\U3046\U3048\U304a"
)
-------------- array2
[1, 57, 3]
-------------- array3
[5.0, 768.0, 346.0, 0.200000002980232]
-------------- array4
[か, き, さしすせそ, け]
-------------- Dictionary
[number: つ, name: た, address: ち]
ち
[number: つ, name: た, address: マダガスカル]
-------------- for in 1
1
57
3
-------------- for in 2
(number, つ)
(name, た)
(address, マダガスカル)
-------------- for in 3
1
2
3
4
5
6
7
-------------- for in 4
1
2
3
4
5
6
7
8
-------------- for in 5
0 : 5.0
1 : 768.0
2 : 346.0
3 : 0.200000002980232
-------------- for in 6
number : つ
name : た
address : マダガスカル
-------------- for
for : 0
for : 1
for : 2
for : 3
for : 4
for : 5
for : 6
for : 7
for : 8
for : 9