我怎么能实现这个?我认为我的解决方案很脏,我想做得更好.我认为在Ruby中有一种简单的方法可以做到这一点,但我不记得了.我想在Rails中使用它,所以如果Rails提供类似的东西也可以.用法应该是这样的:
fruits = ['banana', 'strawberry', 'kiwi', 'orange', 'grapefruit', 'lemon', 'melon']
# odd_fruits should contain all elements with odd indices (index % 2 == 0)
odd_fruits = array_mod(fruits, :mod => 2, :offset => 0)
# even_fruits should contain all elements with even indices (index % 2 == 1)
even_fruits = array_mod(fruits, :mod => 2, :offset => 1)
puts odd_fruits
banana
kiwi
grapefruit
melon
puts even_fruits
strawberry
orange
lemon
Run Code Online (Sandbox Code Playgroud)
*******编辑*******
对于那些想知道的人,这就是我最终做到的:
在rails项目中,我创建了一个新文件config/initializers/columnize.rb,如下所示:
class Array
def columnize args = { :columns …Run Code Online (Sandbox Code Playgroud) 当我继承std::exception以定义我自己的异常类型时,我需要覆盖what()具有以下签名的方法:
virtual const char* what() const throw();
Run Code Online (Sandbox Code Playgroud)
这对我来说肯定很奇怪,就像签名中有两个方法名称一样.这是一些非常具体的语法,就像纯虚方法一样,例如:
virtual int method() const = 0;
Run Code Online (Sandbox Code Playgroud)
或者这是一个功能,也可能以某种方式在另一个上下文中使用?如果是这样,它可以用于什么?
我想在method_missing抛出时为我的模型添加一些行为,但我知道searchlogic已经将它用于它们的命名搜索范围.
扩展method_missing功能的正确方法是什么?有没有办法对method_missing触发器做出反应?就像如何将函数绑定到javascript中的事件一样?
这是一些c ++代码的摘录,我将在几天内详细解释:
std::vector<int> vct(8, 5);
std::generate(vct.begin(), vct.end(), &rand);
std::copy(vct.rbegin(), vct.rend(),
std::ostream_iterator<int>(std::cout, "\n"));
Run Code Online (Sandbox Code Playgroud)
我想我理解它的一切,除了那个微小的神秘和兰德.究竟是做什么的?我的意思是它显然产生某种伪随机数,但它们总是保持不变.&rand来自哪里?它是什么样的表达方式?它产生的价值来自哪里?我有点困惑......
此外,我既没有在代码中找到任何其他出现的"rand"一词,也没有看到任何可能与随机数有关的代码.这让我很奇怪,因为我(非常有限)的c ++经验表明,只有很少的东西可以简单地工作,而不必事先声明或包含.
谢谢你帮忙!
我有一个Subscription带有price字段的模型,我上一次迁移引入了一个名为的字段per_month_price.我正在寻找一种方法,可以比简单更快地更新记录.each {...}.例如:
Subscription.update_all {|c| c.per_month_price = c.price/12}
我需要检查数组的任何2个元素的总和是否等于给定的数字.这就是我提出的,但它似乎没有进行比较
def sum_comparison(int_array, x)
n = int_array.length
(0..n).each do |i|
(1..n).each do |j|
if ((int_array[i].to_i + int_array[j].to_i) == x)
return true
else
return false
end
end
end
end
Run Code Online (Sandbox Code Playgroud) 最近我开始学习Ruby.我正在练习逻辑运算符irb,我得到了这些结果,我不明白.你能帮我澄清一下这些例子吗?
1 and 0
#=> 0
0 and 1
#=> 1
0 && 1
#=> 1
Run Code Online (Sandbox Code Playgroud) 如何在具有字符串键的地图上进行模式匹配?
iex(1)> my_map = %{"key1" => "var1"}
%{"key1" => "var1"}
iex(2)> %{aa => bb} = my_map
** (CompileError) iex:2: illegal use of variable aa inside map key match, maps can only match on existing variable by using ^aa
(stdlib) lists.erl:1354: :lists.mapfoldl/3
iex(2)> %{"aa" => bb} = my_map
** (MatchError) no match of right hand side value: %{"key1" => "var1"}
Run Code Online (Sandbox Code Playgroud) 我希望在递归调用期间保持变量的值为静态,例如,如果foo是一个name作为参数的函数,我想name 将第一次调用的值保存到foo变量中,变量将保留每个值中的值递归调用foo.
def run_app (name, startr)
if startr==1
constant_var=name
end
some_name = modify name
diff = compare some_name, constant_var
# recursive call
run_app some_name, 0
end
Run Code Online (Sandbox Code Playgroud)
第一次调用就像,run_app "john", 1我希望在调用期间保留constant_var的值.我怎样才能做到这一点?
我正在尝试用简单的方法编写测试Ada.Text_IO.Put.为简单起见,这是一个我想测试的组合方法:
procedure Say_Something is
begin
Put("Something.");
end Say_Something;
Run Code Online (Sandbox Code Playgroud)
在我的AUnit测试中,我有:
procedure Test_Put (T : in out Test) is
pragma Unreferenced (T);
use Ada.Text_IO;
Stdout : constant File_Type := Standard_Output;
Put_File_Name : constant String := "say_something_test.txt";
Put_File : File_Type;
Expected : constant String := "Something.";
begin
-- Create the output file and redirect output
Create (Put_File, Append_File, Put_File_Name);
Set_Output (Put_File);
Say_Something;
-- Redirect output to stdout and close the file
Set_Output (Stdout);
Close (Put_File);
-- Read file
declare
File_Size : …Run Code Online (Sandbox Code Playgroud)