如题,大家好像讨论的比较多的都是 rails,或者其他的一些 web 框架,除此之外,还有一些比较有趣的应用吗?
我在学习《R 和 RUBY 数据分析之旅第 3 章》,代码如下: class Restroom attr_reader :queue attr_reader :facilities
def initialize(facilities_per_restroom=3) @queue=[] @facilities=[] @facilities_per_restroom.times{@facilities<<Facility.new} end
def enter(person) unoccupied_facility=@facilities.find{|facility| not facility.occupied?} if unoccupied_facility unoccupied_facility.occupy person else @queue<<person end end
def tick @facilities.each{|f| f.tick} end end
class Facility def initialize @occupier = nil @duration = 0 end def occupy(person) unless occupied? @occupier = person @duration =1 Person.population.delete person true else false end end
def occupied? not @occupier.nil? end
def vacate
Person.population<<@occupier
@occupier = nil
end
def tick if occupied? and @duration > @occupier.use_duration vacate @duration = 0 elseif occupied? @duration +=1 end end end
class Person @@population = [] attr_reader :use_duration attr_accessor :frequency
def initialize(frequency=4,use_duration=1) @frequency= frequency @use_duration=use_duration end
def self.population @@population end
def need_to_go? rand(DURATION)+1<=@frequenty end end
require 'csv' frequency=3 facilities_per_restroom=3 use_duration=1 population_range=10..600
data={} population_range.step(10).each do |population_size| Person.population.clear population_size.times{Person.population<<Person.new(frequency,use_duration)} data[population_size]=[] restroom=Restroom.new facilities_per_restroom DURATION.times do |t| data[population_size]<<restroom.queue.size queue=restroom.queue.clone restroom.queue.clear unless queue.empty restroom.enter queue.shift end Person.population.each do |person| if person.need_to_do restroom.enter person end end restroom.tick end end
CSV.open('simulation1.csv','w') do |csv| lbl=[] population_range.step(10).each{|population_size|lbl<<population_size} csv<<lbl DURATION.time do |t| row =[] population_range.step(10).each do |population_size| row <<data[population_size][t] end csv <<row end
end
运行结果如下: restroom.rb:90:in `step': no block given (LocalJumpError) from restroom.rb:90
第 90 行:population_range.step(10).each do |population_size|
哪里错了,请指点!
#3 楼 @oicuicu 需要提问题的话单独发个帖吧。顺便最好看一下发帖界面右侧的帮助提示,看应该能怎样更好地贴代码。实际上一般在论坛贴代码都很难读,最好发到 http://gist.github.com/
@chunlea 原来这个东西论坛讨论过,感觉好玩。我对编译原理不熟悉,一直很好奇像这种不同语言的代码转编译技术难度怎样,印象中原来 flash 不也做过转成 ios 本地代码的东西,但被封杀了
@Rei 要翻墙吗?视频都打不了,不知道视频有什么特别的。“Expert recording services for conferences, seminars, and workshops.”,应该不止是视频分享吧?
@drift_ic 可以看看这篇文章http://writings.chunlea.org/articles/7-rubymotion-runtime-guide。反正感觉跟传统的代码转编译还是有些不同的。