考虑用法
double Total = 0.00;
string destination = Console.ReadLine();
int Destination = Convert.ToInt16(destination);
switch (Destination)
{
case 1:
{
Console.WriteLine("you have selected Barbados! Heres the following excursions you can do!");
Console.WriteLine("_________________________!Excursions!_________________________________");
Console.WriteLine(" ");
Console.WriteLine("Snorkeling in Barbados bay: $199.99 Per Person");
Console.WriteLine("Press 1");
Console.WriteLine("Barbados Tour Bus Adventure: $59.99 Per Person");
Console.WriteLine("Press 2");
Console.WriteLine("Barbados Shopping Extravanga: $199.99 Per Person");
Console.WriteLine("Press 3");
Console.WriteLine("_________________________!Excursions!_________________________________");
Console.WriteLine(" ");
Console.WriteLine("What is your choice?");
string barbadosanswer = Console.ReadLine();
int barbadosAnswer = Convert.ToInt32(barbadosanswer);
switch(barbadosAnswer)
{
case 1:
Total + 199.99;
//heres where id add to Double total but it wont allow me to it needs a declaration that it is being added?
Console.WriteLine("You have selected Snorkeling in Barbados bay! Your total is currently " + Total);
break;
}
我想用自定义对象进行模拟
Rails.cache.fetch("key", expires_in: 2.minutes) do
do_something_to_get_value
end
对于class FakeRedis
def initialize() @data = {} end
def clear() @data = {} end
def write(key, val, *args) @data[key] = val end
def read(key, *args) @data[key] end
def expire(key, *args) @data.delete(key) end
def fetch(key, *args) @data[key] end
end
(nil
过期或从不存在)的情况,我该如何重写key
以便随后调用代码块并保存到fetch
?
答案 0 :(得分:1)
def fetch(key, *args)
return(@data[key]) if @data.kas_key?(key)
@data[key] = yield if block_given?
end
yield
是将执行该块的方法。
答案 1 :(得分:1)
碰巧,ruby Hash有一个不错的fetch
方法,除了您必须在代码块中显式设置密钥外,它的功能几乎相同:
class FakeRedis
def fetch(key, *, &block)
if block
@data.fetch(key) { |h, k| h[k] = block.call }
else
@data[key]
end
end
end
答案 2 :(得分:0)
以这种方式重写:
def fetch(key, *args) @data[key] ||= yield if block_given? end