我正在编写一个 Ruby 库,它有一个模块,里面有一堆类。其中许多类需要通过调用脚本来使用和修改,但我不希望(某些)初始化程序可见/可调用:
module MyLib
class Control
def initialize
# They can use this
end
def do_stuff
Helper.new('things')
end
end
class Helper
# Shouldn't be visible
def initialize(what)
@what = what
end
def shout
@what
end
end
end
c = MyLib::Control.new
h = c.do_stuff
p h.shout
# => "things"
# ^ All of this is desired
# v This is undesirable
p MyLib::Helper.new('!')
# => <MyLib::Helper @what='!'>
Run Code Online (Sandbox Code Playgroud)
如果这是一件简单的事情,那么我也希望生成的 RDoc 甚至不包含该类的.new方法Helper。有任何想法吗?
谢谢阅读!
我正在尝试使用google_search ruby库(代码如下),但它抱怨'这cattr_accessor是一种未定义的方法' - 任何想法为什么会这样或我如何解决它?
require 'rubygems'
require 'google_search'
GoogleSearch.web :q => "pink floyd"
Run Code Online (Sandbox Code Playgroud) 我知道SOUNDEX和(双)Metaphone,但这些不让我测试整个单词的相似性 - 例如"Hi"听起来非常类似于"Bye",但这两种方法都会标记它们完全不同.
Ruby中是否有任何库或您知道的任何方法能够确定两个单词之间的相似性?(布尔值是/不相似,或数字40%相似)
编辑:如果有一种简单的方法可以"插入"不同的方言或语言,可以获得额外奖励积分!
我有一个生成子类实例的主类实例,这些子类需要将一些方法调用转发回主实例.
目前我的代码看起来像这样,但感觉我应该能够更有效地做同样的事情(也许使用method_missing?)
class Master
def initalize(mynum)
@mynum = mynum
end
def one_thing(subinstance)
"One thing with #{subinstance.var} from #{@mynum}"
end
def four_things(subinstance)
"Four things with #{subinstance.var} from #{@mynum}"
end
def many_things(times,subinstance)
"#{times} things with #{subinstance.var} from #{@mynum}"
end
def make_a_sub(uniqueness)
Subthing.new(uniqueness,self)
end
class Subthing
def initialize(uniqueness,master)
@u = uniqueness
@master = master
end
# Here I'm forwarding method calls
def one_thing
master.one_thing(self)
end
def four_things
master.four_things(self)
end
def many_things(times)
master.many_things(times,self)
end
end
end
m = Master.new(42)
s = m.make_a_sub("very")
s.one_thing === …Run Code Online (Sandbox Code Playgroud) 谁能想到一种方法来使用ol/ lilist中的数字来标记图像?
<ol>
<li><img /></li>
<li><img /></li>
<li><img /></li>
</ol>
Run Code Online (Sandbox Code Playgroud)
应用一些CSS应输出以下内容:
------ ------ ------
| | | | | |
| | | | | |
| 1| | 2| | 3|
------ ------ ------
Run Code Online (Sandbox Code Playgroud)
每个正方形都是小型的图片.
我知道我可以在其中插入一个li带有数字的新元素并根据需要进行操作,但我想用内置ol编号来完成.
我很难理解 CouchDB 的链接文档功能。
我有两个types数据存储在一个 CouchDB 数据库中:
{
"id":"1",
"type": "track",
"title": "Bohemian Rhapsody"
}
{
"id":"2",
"type": "artist",
"name": "Queen",
"tracks": ["1"]
}
Run Code Online (Sandbox Code Playgroud)
我的印象是我可以编写如下所示的视图并发出以下文档:
{
"id":"2",
"type": "artist",
"name": "Queen",
"tracks": [
{
"id":"1",
"type": "track",
"title": "Bohemian Rhapsody"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试这种观点,但它并没有按照我期望的方式工作:
function(doc) {
if(doc.type == 'artist') {
var tracks = [];
for(var i = 0; i < doc.tracks.length; i++) {
tracks.push({_id:doc.tracks[i]});
}
newdoc = eval(uneval(doc));
newdoc.tracks = tracks;
emit(doc._id,newdoc);
}
}
Run Code Online (Sandbox Code Playgroud)
这里的例子:http ://jphastings.iriscouch.com/_utils/database.html?music/_design/test/_view/linked …
如果我有一个写为字符串的正整数,比如"three"或"forty nine",是否有一种简单的方法将其转换为整数?
我很高兴使用语言学来转换另一种方式,但我从未尝试过这种方式!
Chingu示例看起来像这样:
require 'rubygems'
require 'chingu'
class Game < Chingu::Window
def initialize
super
@player = Player.new
end
end
class Player < Chingu::GameObject
def initialize(options = {})
super(options.merge(:image => Gosu::Image["player.png"])
end
end
Game.new.show
Run Code Online (Sandbox Code Playgroud)
如果我想用线而不是图像绘制Player对象,我将如何进行此操作?
以下代码看似直观,但我无法让它工作!
class Player < Chingu::BasicGameObject
def initialize(options = {})
super
@radius = options[:radius]
@c = Gosu::Color.new(0xffff0000)
end
def draw
$window.draw_rect([@x-1,@y+1,@x+1,@y+1,@x+1,@y-1,@x+1,@y+1],@c,1)
end
end
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?
有ThingS IN PlaceS的,我希望找到.一个Thing可以在许多不同的Places中,并且许多Things可以在一个中Place.
class Thing < ActiveRecord::Base
has_and_belongs_to_many :places
end
class Place < ActiveRecord::Base
has_and_belongs_to_many :things
end
Run Code Online (Sandbox Code Playgroud)
我想记录Find我的Users,以便我知道他们在哪里找到了什么.
class Find < ActiveRecord::Base
belongs_to :user
belongs_to :places_thing # Is this depluralization correct?
end
class User < ActiveRecord::Base
has_many :finds
# Now, how can I link in the Things the user has found? Like this?
has_many :found_things_in_places, :class_name => :places_things, :through => :finds
has_many :things, :through => …Run Code Online (Sandbox Code Playgroud) 如果Users有分数,假设标准定位,我怎样才能得到用户等级:
require 'active_record'
class User < ActiveRecord::Base
def rank
# ???
end
end
User.all
# => [<User id:1 points:100>, <User id:2 points:400>, <User id:3 points:100>, <User id:4 points:250>]
User.find_by_id(2).rank
# => 1
User.find_by_id(3).rank
# => 3
User.find_by_id(1).rank
# => 3
Run Code Online (Sandbox Code Playgroud)
我得到了明显的印象,这将是数据库密集型.我并不太担心,但显然需要较少处理能力的解决方案才是赢家!
如果给予具有相同数量的点的两个用户相同的等级太复杂,我准备接受上面的用户1可以是等级3,而用户3可以是等级4.
编辑:
我实际上是在一个单独的课堂上学习gmy乐谱 - 因为每个事件都需要一定数量的积分.
class Event < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :events
def points
events.sum(:amount)
end
def rank
# This seems a bit ham-handed
Event.count_by_sql(['select count(*) from (select sum(amount) as points …Run Code Online (Sandbox Code Playgroud) ruby ×8
activerecord ×2
class ×2
couchdb ×1
css ×1
html ×1
javascript ×1
libgosu ×1
linguistics ×1
module ×1
nlp ×1
phoneme ×1
sql ×1