shitou's blog 
Home About Feed | 十人族 MIX BOY 塌客

Tags "ruby"

18
Jan

Ruby和Erlang的交互 by shitou

0

Ruby的速度慢是公认的(虽然我并不想面对这一点),所以在遇到一些需要大量运算的业务时就会遇到瓶颈,这时可以把这部分的逻辑运算交給其他相对运算较快的语言来做,ruby只用得到返回结果就行了,

 

可以使用Rubyinline在ruby代码种嵌入C, 也可以使用Erlectricity来实现与Erlang的对接,

 

除了这种办法之外还可以使用后台运行来处理消耗资源的任务,当然这只能针对于及时性不高的业务。

 

参考:

http://www.infoq.com/cn/news/2009/09/haskell-ruby-hubris

http://github.com/mojombo/erlectricity

Tags: ruby,Erlang

2010-01-18 15:47:57, 383 reviews

send to mailbox

Your email:

21
May

服务器监控小脚本 by shitou

0

监控服务器存活的脚本,用ruby写的,需要安装有action_mailer gem,

功能:

检测主机的80端口是否可用

失败次数记录,超过指定次数判定主机Down掉

发送通知邮件到指定邮箱


在前面的日志中有说明怎样连接gmail来发送邮件的


require 'rubygems'
require 'action_mailer'
require 'ping'
require 'fileutils'
require 'tlsmail' #need install tlsmail gem to support TLS connect
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

#发送通知from账户设置
ActionMailer::Base.smtp_settings = {
  :address => 'smtp.gmail.com',
  :port => '25',
  :domain => 'gmail.com',
  :user_name => 'username',
  :password => 'passwd',
  :authentication => :login
}

#recp: 收到通知的邮箱地址,可以填多个地址,逗号分隔
#sub: 邮件主题
#bo: 邮件内容
class SimpleMailer < ActionMailer::Base
  def simple_message(recp, sub, bo)
    from 'sender@gmail.com'
    recipients recp
    subject sub
    body bo
  end
end

class ServerMonitor
  def initialize
    #config
    @hit = 5  #最大失败次数,达到后才会发送邮件,避免误报
    @tmp = "/tmp/server_monitor_tmp"  #计数器地址
    @recp = 'mytake6@gmail.com, std8545@yahoo.com.cn'  #收信的邮箱地址
    @host = ['www.ccok.me', 'www.google.com']  #需要监控的主机
    
    #do not touch these
    @hosts = Struct.new(:host, :status)
    @servers, @msg = [], []
    @host.each { |h| @servers << @hosts.new(h, true) }
    
    FileUtils.mkdir(@tmp) unless File.exists? @tmp
  end
  
  def run
    @servers.each do |s| 
      num = Ping.pingecho(s.host, 5, 80) ? 0 : rf(s.host) + 1
      File.open(dest(s.host), 'w') { |f| f.puts num }
    end

    @servers.each do |e|
      if rf(e.host) == @hit
        e.status = false
        File.open(dest(e.host), 'w') { |f| f.puts 0 }
      end
    end
    
    send_mail
  end
  
  def rf file
    return 0 unless File.exist?(dest(file))
    File.open(dest(file), 'r').readlines.to_s.chomp.to_i
  end
  
  def dest host
    "#{@tmp}/#{host}"
  end
  
  def send_mail
    @servers.each { |m| @msg << m.host unless m.status }
    SimpleMailer.deliver_simple_message @recp, 
        "server monitor", 
        @msg.join(', ') + " is Down!!! " unless @msg.empty?
  end
end

#go
ServerMonitor.new.run


可以放到crontab中每2分钟运行一次


Tags: 监控,ruby,linux

2009-05-21 14:20:44, 857 reviews

send to mailbox

Your email:

22
Nov

ruby写的一个随机发扑克类 by shitou

2

最近用ruby写了一个随机发扑克的类, 可以指定参加的player个数

 

class Card54
  
  @@given = [] #the cards already given
  @@player_num = nil #players number
  @@player_cards = {} #cards players get
  @@now_turn = 0 #now player's turn
  
  @@cards = (1..54).to_a #total cards
  
  def initialize(player_num=4)
    @@player_num = player_num
    @@cards.length.times { run }
  end
  
  def run
    raise 'no card avaiable' if @@given.length == @@cards.length
    card = rand_card(@@cards - @@given)
    @@given << card
    @@now_turn = 0 if @@now_turn == @@player_num
    give @@now_turn.next, card
    @@now_turn = @@now_turn.next
  end
  
  def player_cards
    p @@player_cards
  end
  
  private
  def rand_card c
    c[rand(c.length)]
  end
  
  def give turn, card
    if @@player_cards[turn].nil?
      @@player_cards[turn] = card.to_s
    else
      @@player_cards[turn] = @@player_cards[turn] + ',' + card.to_s
    end
  end
end
 
#require 'card54'
#play = Card54.new 4
#play.player_cards

这样就支持Card54.new.player_cards了

 

class Card54
  
  attr_reader :player_cards, :player_num
  
  def initialize(player_num=4)
    @given = [] #the cards already given
    @player_cards = {} #cards players get
    @now_turn = 0 #now player's turn
    @cards = (1..54).to_a #total cards
    
    @player_num = player_num
    @cards.length.times { run }
  end
  
  def run
    raise 'no card avaiable' if @given.length == @cards.length
    card = rand_card(@cards - @given)
    @given << card
    @now_turn = 0 if @now_turn == @player_num
    give @now_turn.next, card
    @now_turn = @now_turn.next
  end
  
  private
  def rand_card c
    c[rand(c.length)]
  end
  
  def give turn, card
    if @player_cards[turn].nil?
      @player_cards[turn] = card.to_s
    else
      @player_cards[turn] = @player_cards[turn] + ',' + card.to_s
    end
  end
end

 

Tags: ruby

2008-11-22 23:15:28, 595 reviews

send to mailbox

Your email:

06
Aug

进程和线程 by shitou

0

最近在看ruby的线程这块的东西,就找了些资料关于linux进程和线程的,因为ruby的线程是基于linux和unix的,所以机制也和linux一样,

Introduction to threads and processes
进程和线程简介
Programs consist of a number of processes, each of which contains one or more conceptually concurrent threads of execution.
程式包含了若干进程,每一个进程包含了一个或多个概念上知执行的线程。
A thread is the unit of execution within a process. Every time a process is initialised, a primary thread is created. For many applications the primary thread is the only one that the application requires; however, processes can create additional threads.
线程是进程的执行单元。当进程被初始化后,主线程就被创建了。对于绝大多数的应用程式来说,通常仅需要有一个主线程。尽管如此进程也能够创建额外的线程。
Each user process has its own private address space, i.e. a collection of memory regions which that process can access. A user process cannot directly address memory areas in the address space of another process. There is also a special process, the Kernel process, whose threads run at supervisor privilege level. This process normally contains two threads:
每一个用户进程拥有自己私有的地址空间,也就是说,进程拥有一定的可被其访问的内存区域。一个用户进程不能够直接访问其他进程的地址空间。另外更有一个特别的进程,内核进程,他运行在终极用户权限模式。这个进程通常包括两个线程:
the Kernel server thread, which is the initial thread whose execution begins at the reset vector, and which is used to implement all Kernel functions requiring allocation or deallocation on the Kernel heap. This is the highest priority thread in the system.
Kernel server (内核服务器)线程:是个初始的进程,在系统启动时就已存在。他能够在heap执行由核心函数请求的内存分配或内存的重分配。这是系统中具备最高权限的线程。
the null thread, which runs only when no other threads are ready to run. The null thread places the processor into idle mode to save power.
null (空)线程:当系统中没有其他可运行的线程时这个线程就开始运行,null 线程使处理器处于空闲状态,减少耗电。
Threads execute individually and are unaware of other threads in a process. The scheduling of threads is pre-emptive, i.e. a currently executing thread may be suspended at any time to allow another thread to run.
线程是单独运行的,他且并不知道进程中更有其他线程存在。线程的执行是抢占式的,也就是说,当前运行的线程在任何时候都可能被挂起,以便另外一个线程能够运行。
Each thread is assigned a priority; at any time, the thread running is the highest priority thread which is ready to run. Threads with equal priority are time-sliced on a round-robin basis. Context switching between threads involves saving and restoring the state of threads. This state includes not only the processor registers (the thread context) but also the address space accessible to the thread (the process context). The process context only needs switching if a reschedule is between threads in different processes.
每一个线程都配置了优先限权;在 任何时候,只要线程已准备就绪,具备高优先权的线程总是首先运行。假如线程具备相同的悠闲权,则根据时间片进行轮转调度。上下文的转换包括了保存和恢复线 程状态。这个状态不但仅包含了处理器寄存器(进程上下文)而且还包含了线程可访问的地址空间(进程上下文)。只有在重新调度是在两个进程间进行的时候,进 程上下文才被转换。
Compare this with active objects which allow non pre-emptive multi-tasking within a single thread.
把这个和活动对象比较,活动对象允许在一个线程中实现非强占式的多任务调度。
A thread can suspend, resume, panic and kill another thread.
一个线程能够被挂起,唤醒、异常抛出和结束其他线程。
When a thread is created it is put into a suspended state, it does not begin to run until that thread’s Resume() member function is called.
线程被创建以后,他处于挂起状态。他没有马上进入运行状态,直到他的Resume()成员函数被调用。
When a thread is created, it is given the priority EPriorityNormal by default. The fact that a thread is initially put into a suspended state means that the thread priority can be changed (RThread::SetPriority()) before the thread is started (RThread::Resume()).
线程创建以后,他具备EPriorityNormal的默认优选级。线程被初始化并处于挂起状态,这意味着在线程开始运行前,线程的运行优先级能够被改变(通过调用RThread::SetPriority())


就是线程被创建的时候默认是没有运行的,即处于挂起(suspend)状态,所以要进行唤醒(wakeup,或者是join)才能运行,在唤醒前可以调整线程的优先级

Tags: 线程,进程,ruby

2008-08-06 09:43:03, 578 reviews

send to mailbox

Your email:

29
Jul

关于yield的使用 by shitou

0

一直对ruby中yield的使用比较迷惑,今天抽了点时间好好看了下,
下面是内置类File ::open的源码

def File.open(name, mode = "r")
    f = os_file_open(name, mode)
    if block_given?
        begin
            yield f
        ensure
            f.close
        end
        return nil
    else
        return f
    end
end


这个方法非常简洁明了,block_given?判断是否传递了代码块,有的话就把文件句柄传给代码块进行操作,这个地方就是

yield f

我觉得可以这样理解yield的行为:把yield后面的参数传递给后面的代码块作为参数。
yield的作用可以这样理解:拿学生举个例子,每个学生交的学费是一样的,所以处理交学费的流程就放在方法中,但是每个学生的要花费的生活费不一样,要考虑很多的因素,所以把特殊的因素放在代码块中操作,这样就可以即统一又有区别的计算出总的花费。

 

另一个例子

 

def c(v, &block)

    return v unless v.is_a? Array

    v.each &block

end

c([1,2,3]) { |x| p x + 1 } #2, 3, 4

 

def c(v, &block)

    return v unless v.is_a? Array

    v.each { |x| yield x }

end

c([1,2,3]) { |x| p x + 1 } #2, 3, 4

 

Tags: ruby,yield

2008-07-29 17:54:34, 1041 reviews

send to mailbox

Your email:

23
Jul

[转载]5个有用的ruby gems by shitou

6

Check out the top 5 most useful ruby gems that you may not have heard of!

Mailfactory

Mailfactory is a ruby library designed to allow the simple creation of emails.  It offers the following features:

  • Plain text and HTML body parts
  • Simple attachments
  • MIME-type guessing for attachments
  • Attachments from arbitrary IO objects
  • Custom headers
  • RFC valid email generation
  • Simple API

To Install:

gem install mailfactory

Example:

  1. require 'rubygems'  
  2. require 'net/smtp'  
  3. require 'mailfactory'  
  4.   
  5. #Connect to SMTP Server  
  6. smtp = Net::SMTP.new('mail.host.com', 25)  
  7. smtp.start('mydomain.com')  
  8.   
  9. #Construct Mail Message   
  10. mail = MailFactory.new()  
  11. mail.to = 'foo@monkey.com'  
  12. mail.from = 'jason@dzone.com'  
  13. mail.subject = 'An email from Ruby'  
  14. mail.add_attachment('/path/to/file')  
  15. mail.html = "

    Hello From Ruby!

    Mailfactory is nice

    "
      
  16.   
  17. #Construct SMTP Message  
  18. smtp.send_message mail.construct, 'foo@monkey.com', 'jason@dzone.com'  
  19.   
  20. #Send this (and all other) message  
  21. smtp.finish()      
require 'rubygems'
require 'net/smtp'
require 'mailfactory'

#Connect to SMTP Server
smtp = Net::SMTP.new('mail.host.com', 25)
smtp.start('mydomain.com')

#Construct Mail Message  
mail = MailFactory.new()
mail.to = 'foo@monkey.com'
mail.from = 'jason@dzone.com'
mail.subject = 'An email from Ruby'
mail.add_attachment('/path/to/file')
mail.html = "

Hello From Ruby!

Mailfactory is nice

"

#Construct SMTP Message
smtp.send_message mail.construct, 'foo@monkey.com', 'jason@dzone.com'

#Send this (and all other) message
smtp.finish()  

    

Feed-Normalizer

Feed-Normalizer normalizes all RSS Feed formats into a generic class. This allows you to access the same properties from any RSS Feed it parses without worrying about the underlying format of the RSS source.

To Install:
gem install feed-normalizer

Example:

  1. require 'rubygems'  
  2. require 'feed-normalizer'  
  3.   
  4. #Define URL and Parse Feed  
  5. feed_url = 'http://rss.slashdot.org/Slashdot/slashdot'  
  6. rss = FeedNormalizer::FeedNormalizer.parse open(feed_url)  
  7.   
  8. #Quit if no articles  
  9. exit unless rss.entries.length > 0  
  10.   
  11. #Read entries  
  12. rss.entries.each do |entry|  
  13.     title = entry.title  
  14.     body = entry.content  
  15.     authors = entry.authors.join(', ') rescue ''  
  16.     entry_url = entry.urls.first  
  17.   
  18.     #Your Logic Here  
  19. end 

 

 

FasterCSV

FasterCSV is significantly faster than CSV and arguably has an improved CSV interface.

To Install
gem install fastercsv

Example:


  1. require 'rubygems'  
  2. require 'fastercsv'  
  3.   
  4. #Read a CSV File  
  5. FasterCSV.foreach('/path/to/file') do |row|  
  6.     element1 = row[0]  
  7.     element2 = row[2]  
  8.     element3 = row[3]  
  9. end  
  10.   
  11. #Write a CSV File  
  12. FasterCSV.open('/path/to/file') do |csv|  
  13.     csv << ["an", "array", "of", "data"]  
  14. end  
require 'rubygems'
require 'fastercsv'

#Read a CSV File
FasterCSV.foreach('/path/to/file') do |row|
  element1 = row[0]
  element2 = row[2]
  element3 = row[3]
end

#Write a CSV File
FasterCSV.open('/path/to/file') do |csv|
  csv << ["an", "array", "of", "data"]
end

    

Bishop

Bishop is a Bayesian classifier library for Ruby. With Bishop you can effectively train your program to recognize any number of things.

To Install:
gem install bishop

Example:

  1. require 'rubygems'  
  2. require 'bishop'  
  3.       
  4. #Initialize the classifier  
  5. classifier = Bishop::Bayes.new  
  6. classifier.load('spam_or_no_spam.yml') if File.file?('spam_or_no_spam.yml')  
  7.   
  8. #Train the classifier to recognize spam email  
  9. classifier.train('spam', 'Amazing results in a few weeks!')  
  10. classifier.train('spam', 'Get your free viagra!')  
  11. classifier.train('spam', 'A credit card offer!')  
  12.   
  13. #Train the classifier to recognize legit email  
  14. classifier.train('not spam', 'Your payment update')  
  15. classifier.train('not spam', 'Scheduled Maintenance Reminder')  
  16. classifier.train('not spam', 'Can you pick the kids up today?')  
  17.   
  18. #Save classication file so your program can learn  
  19. classifier.save('spam_or_no_spam.yml')  
  20.   
  21. #Guess if an email is spam or not  
  22. guess = classifier.guess('How would you like a free sample of viagra?')  
require 'rubygems'
require 'bishop'
  
#Initialize the classifier
classifier = Bishop::Bayes.new
classifier.load('spam_or_no_spam.yml') if File.file?('spam_or_no_spam.yml')

#Train the classifier to recognize spam email
classifier.train('spam', 'Amazing results in a few weeks!')
classifier.train('spam', 'Get your free viagra!')
classifier.train('spam', 'A credit card offer!')

#Train the classifier to recognize legit email
classifier.train('not spam', 'Your payment update')
classifier.train('not spam', 'Scheduled Maintenance Reminder')
classifier.train('not spam', 'Can you pick the kids up today?')

#Save classication file so your program can learn
classifier.save('spam_or_no_spam.yml')

#Guess if an email is spam or not
guess = classifier.guess('How would you like a free sample of viagra?')

  

Linguistics

Linguistics is a generic, language-neutral framework for extending Ruby objects with linguistic methods. This is easily one of the coolest gems. There is simply so much you can do with the Linguistics module.

To Install:
gem install linguistics

Example:

  1. require 'rubygems'  
  2. require 'linguistics'  
  3.   
  4. "book".en.a  
  5. # => "a book"  
  6.   
  7. "runs".en.present_participle  
  8. # => "running"  
  9.   
  10. 5.en.ordinal  
  11. # => "5th"  
  12.   
  13. 2004.en.numwords  
  14. # => "two thousand and four"  
  15.   
  16. animals = %w{dog cow ox chicken goose goat cow dog rooster llama  
  17. pig goat dog cat cat dog cow goat goose goose ox alpaca}  
  18. puts "The farm has: " + animals.en.conjunction  
  19.   
  20. # => The farm has: four dogs, three cows, three geese, three goats,  
  21. two oxen, two cats, a chicken, a rooster, a llama, a pig,  
  22. and an alpaca 
Tags: ruby,gems

2008-07-23 19:49:32, 1164 reviews

send to mailbox

Your email:

23
Jul

[转载]Using temporary files in Ruby - Tempfile.new by shitou

0

一个创建临时文件的ruby内置class

For certain programming solutions, you might need a temporary file. Different operating systems store temporary files in different locations. Also you don’t want to explicitly name the file since that is irrelevant in the program. How do you ensure that there are no filename conflicts? The solution for all these is to use library for temp file processing.

Ruby comes a with a default library ‘tempfile’ for handling temporary file creation. Given a filename prefix, it creates a temporary file in the following format.

[prefix]-[process].[unique_number]

This ensures that there is no conflict in creating multiple temporary files in the same directory. Once your program terminates the temporary file is automatically deleted. Also by default, the temporary file is created in the operating system’s temporary folder (In my ubuntu system it is /tmp).

In the following program, a temporary file with prefix ‘random’ to store 50 random numbers is created. When the program terminates you cannot see the temp file. Hence I have added a ‘gets’ which stops for user input. In my case I could see a temporary file named ‘random.5789.0′ under \tmp dir.

 

  1. require 'tempfile'  
  2.   
  3. class TempSample  
  4.   
  5. temp_file = Tempfile.new('random')  
  6. 1.upto(50) do  
  7.   num = rand(50) # random number generation in ruby  
  8.   temp_file.print(num,"\n") # print number and newline to the file  
  9. end  
  10. temp_file.flush # flush it so that you can see it using cat  
  11.   
  12. gets # wait for user input. At this point you can see a temp file /tmp  
  13. # The filename in my case was random.5789.0  
  14.   
  15. end  

require 'tempfile'  class TempSample  temp_file = Tempfile.new('random') 1.upto(50) do   num = rand(50) # random number generation in ruby   temp_file.print(num,"\n") # print number and newline to the file end temp_file.flush # flush it so that you can see it using cat  gets # wait for user input. At this point you can see a temp file /tmp # The filename in my case was random.5789.0  end  

If you want to the temp file in a specific directory, you can pass the directory name as the second parameter to new. For example - Tempfile.new(’new1′,’/usr/home/jay’)

Tags: ruby

2008-07-23 10:35:52, 860 reviews

send to mailbox

Your email:

Tags

tenerer MooseFS gearman-ruby Gearman MongoDB MochiChat TCP U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • iPhone[17]
  • Erlang[8]
  • google[9]
  • 生活[41]
  • 音乐[11]
  • 电影[11]
  • linux[24]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[8]
  • ruby[18]
  • ruby on rails[27]
  • 开源[3]

Episode

  • MochiChat
  • iPhoneException
  • shell
  • thread
  • memcache

Recent Comments

  • cancer planeta famosas, minal http://zewero.cen...
  • moskowitz mofos, bedroom http://biiili.centerbl...
  • exploit 3pic, julien http://zewero.centerblog.n...
  • Thanks funny site jp imageboard bbs pthc cp :...
  • essence vidz, escalation http://liili.centerblo...
  • arab black singles, sleepwalking http://biiili....
  • Hello good day freeforum.tw pthc 557 valya pt...
  • upenn hand job, sanglah http://liili.centerblog...
  • Thanks funny site ukrainian nymphet rfd nymph...
  • Jonny was here nymphets land ckw topless nymp...

Popular Posts

  • 准备开始学习Erlang了(恶狼, 二郎..)
  • MySQL Innodb备份
  • Lighttpd配置参数
  • iPhone上的HelloWorld终于跑起来了
  • Etag和Expire

Recommended Posts

  • 再次被和谐-_-
  • 十人族: 上线了
  • MooseFS vs. NFS
  • Mochichat新版本上线
  • Gearman
  • MongoDB入门
  • find使用非业余研究
  • MochiChat: 一个基于erlang的web聊天室(beta)
  • /dev/shm和swap的区别
  • gen_tcp的packet参数
  • Beauty - Mai Kuraki
  • awk多维数组
  • We Lost Google.cn
  • Erlang TCP Server例子
  • Mai Kuraki -永远より ながく

Friends' blogs

  • levy
  • sphance
  • andreas

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 87198
  • 今天访问: 16
  • 日志: 187
  • 评论: 167
  • 音乐: 9
  • 用户: 485


 

just DO NOT support IE

close