ITeye博客

2011 年文章归档

共 35 篇文章

[转]浅谈Ruby on Rails中的include和extend

http://developer.51cto.com/art/200907/132919.htm 从模块引入方法、变量,使得编程变得简单,扩展性愈强,比以往的类的继承更灵活。这样的引入,仿佛将一个方法块,复制了一份放到了你所引用的类或者模块里面。你完全可以将多个互不相干的类中相同的方法拿出来写到一个模块中,这样可以使得代码精简,符合Ruby的设计初衷,而且,使得你的程序更有条理。 Ruby on Rails常见用法 通常引用模块有以下3种情况: 1.在类定义中引入模块,使模块中的方法成为类的实例方法 这种情况是最常见的 直接 include 即可 2.在类定义中引入模块,使模块中的方法成为类…

2011-12-30 02:20 · Ruby

[转]Ruby中的Rake任务详述

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1683808 Rake简介 Rake和Make及Ant Rake的意思是Ruby Make,一个用ruby开发的代码构建工具。Rake的英文意思是耙子,一种很朴实的劳动工具。真的是很贴切,Rake正是一个功能强大、勤勤恳恳的劳动工具。 但是,为什么Ruby需要Rake? Ruby代码不需要编译,为什么需要Rake?其实,与其说Rake是一个代码构建工具,不如说Rake是一个任务管理工具,通过Rake我们可以得到两个好处: 以任务的方式创建和运行脚本 当然,你可以用脚本来创建每一…

2011-12-29 03:47 · Ruby

[转]ruby中的闭包

原文: http://kenbeit.com/posts/87/ruby%E4%B8%AD%E7%9A%84%E9%97%AD%E5%8C%85 感谢作者的辛勤劳动。 # CLOSURES IN RUBY Paul Cantrell http://innig.net # Email: username "cantrell", domain name "pobox.com" # # 翻译: kenshin54 http://kenbeit.com # I recommend executing this file, then reading it alongside its output. #…

2011-12-26 10:42 · Ruby

发发牢骚,关于iteye论坛规则小测验

今天想在论坛回复个帖子,一看自己是新手,还不能回贴,需要完成论坛规则小测验,一点进去看,乖乖,里面有13道题目,大概看了下,如果要完成至少7、8分钟吧,真是抓狂。原来的回贴热情被打了回去,呵呵,算了吧,我也不想完成这个无聊的测验,以后当个潜水员吧,这个博客就当是学习的笔记。

2011-12-25 14:03 · 综合

javascript进阶1

参考: http://ejohn.org/apps/learn 答案在后面。 1、看下面的例子,能看得出输出什么? var a = 5; function runMe(a){ console.log("a = " + a);//a=? function innerRun(){ console.log("b = " + b);//b=? console.log("c = " + c);//c=? } var b = 7; innerRun(); var c = 8; } runMe(6); for ( var d = 0; d < 3; d++ ) { setTimeout(function(…

2011-12-23 05:10 · javascript

javascript回函数的几种调用方法

先来看下基本的知识,对于一个函数的调用,方法有以下几种。 //下面的几种写法是等价的。 alert('something'); this.alert('something'); alert.call(this,'something'); alert.apply(this,['something']); 以实现数组中each的方法做为例子。 第一种方式: Array.prototype.each = function(callback){ for(var i = 0; i< this.length;i++){ callback(this[i]);//参数既为函数名,直接加括号调用。 } } […

2011-12-22 04:39 · javascript

coffeescript+prototypejs使编写nodejs更象ruby-like

首先安装prototype for nodejs npm install prototype 看个例子 prototype = require 'prototype' Object.extend global, prototype (9).times (x)-> console.log x [1,2,3,4].each (x)-> console.log x Person = Class.create #定义一个类 initialize: (name)-> @name = name say: (message)-> "#{@name}:#{message}" Pirate = Class.c…

2011-12-21 12:01 · nodejs · coffeescript

javascript中的bind

http://www.prototypejs.org/api/function/bind In JavaScript, functions are executed in a specific context (often referred to as “scope”). Inside the function the this keyword becomes a reference to that scope . Since every function is in fact a property of some object—global functions are properties…

2011-12-21 04:08 · javascript

ruby中实现闭包

ruby中实现闭包很简单 如果一个方法中返回一个procedure那就构成了一个闭包。 def func x = 0 ->{x += 1 ; p x} end proc = func proc[] #or proc.call() proc[] 输出: 1 2

2011-12-20 09:10 · Ruby

排序算法(javascript版与ruby版)

排序算法(javascript版与ruby版) 合并排序 javascript版本: function merge(left, right){ var result = []; while (left.length > 0 && right.length > 0){ if (left[0] < right[0]){ result.push(left.shift());//把最小的最先取出,放到结果集中 } else { result.push(right.shift()); } } return result.concat(left).concat(right);//剩下的就是合并,这样就排…

2011-12-20 06:04 · 综合

ruby中星号的使用

ruby中星号的使用 1、数字乘法 2 * 3 = 6 2、幂 10**2 = 100 (星号之间不能有空格) 3、字符串扩展 'foo' * 2 = 'foofoo' 4、数组的扩展 ]]*2 = ],1,2,]] 5、创建数组 *a = 1,3,"as" a = 6、数组参数传入时的前缀 a= testFun(*a) == testFun(1 , 2) != testFun() 7、还没发现..

2011-12-20 05:54 · Ruby

[转]ruby的include与extend

原文 http://www.cnblogs.com/rubylouvre/archive/2010/03/31/1701813.html 作者:司徒正美 ruby提供了强大的继承机制,有关继承的方法很多。最简单就是“<<”,但还可以对要继承的东西进行细分,是把它添加到类中呢,还是实例中,这正是本文要介绍的内容。 module Foo def foo puts 'heyyyyoooo!' end end class Bar include Foo end Bar.new.foo # heyyyyoooo! Bar.foo # NoMethodError: undefined method ‘f…

2011-12-20 05:09 · Ruby

[转]Ruby常用的内部变量

Ruby常用的内部变量 原文 http://www.cnblogs.com/rubylouvre/archive/2009/07/23/1529237.html 作者:司徒正美 在ruby程序中,经常会看到一些以$开头的变量,这些不是指我们自己在程序中设置的全局变量,而是指系统内部已经设置好的变量,他们代表了一些特定的意思,下面搜集了一些常用的内部变量,用一些简单的代码说明他们代表的意思: 局部域: 在某一个线程作用域内才能有效,下列也可看做是线程内的局部变量。 PS:这边讲解的几个变量都是于正则匹配相关的,正则匹配过程的代码是一样的,这里不重复输入正则表达式匹配的代码,只在第一个例子里给出…

2011-12-20 03:59 · Ruby

Underscore.js与nodejs相结合

今天发现这个Underscore.js,文档说 http://documentcloud.github.com/underscore/ Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby)。 提供了许多类似ruby风格的函数,这是我所喜欢的。 看几个例子。 安装 npm install underscore 在nodes.js中测试 …

2011-12-14 11:18 · nodejs

NowJS and Reconnects

NowJS and Reconnects 保留 原文http://blog.nowjs.com/ This particular feature has been highly anticipated by the NowJS community for some time. With the switch to Socket.IO 0.6.18, which now has built-in support for socket reconnecting, implementation of this feature was actually quite doable. We've tes…

2011-12-14 09:03 · nodejs

Nodejs中的EventEmitter

EventEmitter是nodejs核心的一部分。很多nodejs对象继承自EventEmitter,用来处理事件,及回调。来看API中的例子 var util = require("util"); var events = require("events");//EventEmitter通过events模块来访问 function MyStream() {//新建一个类 events.EventEmitter.call(this); } util.inherits(MyStream, events.EventEmitter);//使这个类继承EventEmitter MyStream.p…

2011-12-14 04:57 · nodejs

nodejs+nowjs实现聊天室

支持多房间,支持私聊,使用nowjs库,服务器端代码不到100行。需要jquery1.7.1和coffee-script.js,读者可自行下载。 重点: 1、nowjs中Group的使用,如何遍历Group内的users。 2、客户端如何异步调用服务器端函数。 3、如何与特定用户通信。 运行 node app.js 在浏览器中访问 http://localhost:8080 app.js require('coffee-script'); require('./chatroomserver'); chatroomserver.coffee coffeescript代码,重点部分已加上注释 f…

2011-12-13 05:05 · nodejs · nowjs · WebSocket

coffeescript与jquery结合

这两者的结合,可提高开发效率。 <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>jQuery meets CoffeeScript</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script src="http://jashkenas.github.com/cof…

2011-12-12 01:35 · coffeescript

nowjs和nodejs结合的简单示例

功能:把服务器端的时间new Date().getTime()传送到客户端,主要测试实时性。轮询间隔为10ms时,能正常显示。可同时打开多个窗口,设置不同轮询时间,各个窗口可独立运行。 服务器端代码: 用coffeescript编写 server.coffee fs = require 'fs' url = require 'url' html = fs.readFileSync(__dirname + '/simple.html') #注意,这里要指向你自己的jquery库地址 jquery = fs.readFileSync(__dirname + '/../node/jquery-1.…

2011-12-10 07:55 · nodejs · WebSocket

nodejs运行coffeescript

首先安装coffee-script npm install coffee-script 在js文件中加入下面代码 require('coffee-script');//引用coffeescript模块 require('./mytest');//指向要运行的coffeescript文件,可加多个文件。 就可以node xx.js,来运行coffeescript文件了。

2011-12-10 03:05 · nodejs