's Blog

Look at the sky, it's so beautiful

Upload File With RestClient

| Comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'rest_client'
cookies = nil
request = RestClient::Request.new(
    :method => :post,
    :url => 'http://www.yoursite.com',
    :payload => {:username=>'your account name',:password=>'password'}
)
request.execute do |response,request,result|
    cookies = response.cookies #save cookies for further use
end

RestClient.Request.new({
    :method => :post,
    :url => 'http://www.yoursite.com/upload.php',
    :cookies => cookies,
    :payload => {
  :multipart => true,
  :filepath =>[File.new('a.jpg'),File.new('b.jpg')]
    }
}).execute do |resp,req,result|
    $stdout.puts resp
end

Reference: https://github.com/adamwiggins/rest-client

Start IRB in Context of an Object

| Comments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
require 'irb'
require 'irb/completion'

class Clazz
    def start_irb
  IRB.setup nil
  IRB.conf[:MAIN_CONTEXT] = IRB::Irb.new.context
  IRB.conf[:PROMPT_MODE] = :SIMPLE
  
  #puts this line outside this method will not work
  require 'irb/ext/multi-irb'

  #clear parameter
  ARGV.clear
  IRB.irb nil, self #<--- pass context
    end
end

Connect to MySQL on Ubuntu VM

| Comments

Step 1: Comment out bind-address on /etc/mysql/my.conf or change it to your VM ip address.

1
#bind-address = 127.0.0.1

Step 2: Create a MySQL user with full/part privileges on all database:

1
2
3
4
5
create user 'username'@'localhost' identified by 'password';
grant all privileges on *.* to 'username'@'localhost' with grant option;

create user 'username'@'%' identified by 'password';
grant all privileges on *.* to 'username'@'%' with grant option;

Step 3: Open your Ubuntu VM, goto Setting->Network->Adapter1 tab->Port Forwarding, add rule: name:mysql protocol:tcp host ip: [blank] host port: 3306 guest ip: [blank] guest port: 3306

click ok

step 4: Connect MySQL in your host machine,

1
2
3
c:>mysql -uusername -ppassword
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

Detect Mouseout in IE678

| Comments

1
2
3
4
5
6
7
8
9
10
var mouseoutEventName = 'mouseout.namespace';
$(document)
  .unbind(mouseoutEventName)
  .bind(mouseoutEventName, function () {
    var e = window.event,
  from = e.relatedTarget || e.toElement;
    if (!from || from.nodeName.toUpperCase() === "HTML") {
  //mouse move out window(document)
    }
});

srcElement-is-undefined-in-firefox11

| Comments

1
2
3
4
element.bind('mousedown',function(e){
  //srcElement is undefined in firefox11 use target in original event instead
  var target = e.srcElement || e.originalEvent.target;
});

Octopress Failed to Read_yaml

| Comments

Octopress failed to read_yaml if your markdown file is GBK encoding. edit Ruby1.9.3\lib\ruby\gems\1.9.1\gems\jekyll-0.11.2\lib\jekyll\convertible.rb as follow

convertible.rb line#29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def read_yaml(base, name)
  self.content = File.read(File.join(base, name)).force_encoding('utf-8')

  if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
    self.content = $POSTMATCH

    begin
      self.data = YAML.load($1)
    rescue => e
      puts "YAML Exception reading #{name}: #{e.message}"
    end
  end

  self.data ||= {}
end

force_encoding will do the trick.

e.keyCode When Ctrl Pressed

| Comments

e.keyCode may be 13 or 10
1
2
3
4
5
6
7
8
9
10
textarea.bind('keypress', function (e) {
    /**
    * Some browsers will send keyCode == 10 instead of keyCode == 13 when using the ctrl modifier key
    * (some browsers will send it even when you aren't using the ctrl modifier key).
    */
    if (e.ctrlKey && (e.keyCode === 13 || e.keyCode === 10)) {
        //do something
        e.preventDefault();
    }
});

Setup Octopress on Windows 7

| Comments

Windows 7 + ruby 1.9.3p125 (2012-02-16) 先安装 ruby 1.9.3和DevKit 然后git clone octopress octopress目录下有个rakefile rake -T查看可用的task rake new_post[“post title”] 在/source/_post下生成一个markdown文件, 编辑markedown文件为blog内容

rake generate将在_deploy目录下生成静态文件 rake deploy将生成的静态文件push到github上

其中在generate的时候可能由于python的版本问题出错:

当增加如下code block 后,

code block
1
```language-name

使用octopress的rake generate生成静态文件时,提示无法打开:

error
1
Could not open library ...python26.dll

原因是python的版本不对。 下载了一个activepython2.7.2.5后,设置好环境变量就OK了 octopress的语法高亮是gem rubypython调用python的pygments做得

参考:

Define Method in JavaScript and Ruby

| Comments

In javascript, function can be attached onto any object; object attributes can be anything,when it is a function, it become a method of that object.

If object is prototype of a function, the attached function becomes instance method of that function(`class’).

In ruby, all methods are reside in instance of Class:

  • intance methods are in object.class;
  • singleton methods are in object.eigenclass. in code class << object.in another word, eigenclass;
  • class methods are in eigenclass of object.class, in code object.class.eigenclass
Object#eigenclass
1
2
3
4
5
class Object
  def eigenclass
      class << self;self;end
  end
end

Define singleton method

define singleton method in javascript
1
2
3
4
var obj = {};
obj.greeting = function(){
  console.log('hi');
};
define singleton method in ruby
1
2
3
4
5
6
7
8
9
10
obj = {}
def obj.greeting1
  puts 'hi'
end

class << obj
  def greeting2
      puts 'hello'
  end
end

Define instance method

define instance method in javascript
1
2
function Clazz(){}
Clazz.prototype.instanceMethod = function(){};
define instance method in ruby
1
2
3
class Clazz
  def instanceMethod;end
end

Define class method

define class method in javascript
1
2
function Clazz(){}
Clazz.classMethod = function(){};
define class method in ruby
1
2
3
4
5
6
7
class Clazz
  define self.classMethod1;end

  class << self
      def classMethod2;end
  end
end

Define attribute

1
2
3
4
5
6
7
8
9
10
11
function Klass(){
  this.name = 'klass'
}
Klass.prototype.attr = 1;

k = new Klass();
k.attr = 123;
k.attr #=> 123
k.name #=> 'klass'
(new Klass()).attr #=> 1
(new Klass()).name #=> 'klass'
1
2
3
4
5
6
7
8
9
10
11
12
Klass = Class.new
k = Klass.new

k.instance_variable_get(:@attr) #=> nil
k.instance_variable_set(:@attr,123) #=> 123
k.instance_variable_get(:@attr) #=> 123

k.attr #=> undefined method `attr'
def k.attr;@attr;end
k.attr #=> 123

Klass.new.attr #=> undefined method `attr'