我想消除「=」符號的特定原因。它可能看起來像這樣:替代「=」的紅寶石方法
cat_that_has_name("Kelly").as(:kelly)
kelly.do_something
這裏的「as」方法用於生成引用我的貓的方法「kelly」。任何人都可以幫助我嗎?
任何建議,將不勝感激。
更新:
約爾格是正確的,我已經添加了一個簡單的測試來證明我的意圖:
require "test/unit"
class AsTest < Test::Unit::TestCase
def setup
@cats = ["Kelly", "Tommy"]
end
def teardown
end
def test_as
kelly1 = get_cat("Kelly")
get_cat("Kelly").as(:kelly2)
assert_equal(kelly1.object_id, kelly2.object_id)
end
private
def get_cat(name)
@cats.each do |cat|
if cat.to_s==name
return cat
end
end
return nil
end
end
你想消除哪個特定'=',在你的例子中看不到一個? – 2010-06-23 17:54:45
如果你的代碼是「之後」,那麼「之前」會是「kelly = cat_that_has_name(」Kelly「); kelly.do_something'? – 2010-06-23 23:22:03
@安德魯,你是對的。 – eric2323223 2010-06-24 02:03:49