我正在尝试对字符串的每个字符执行一个操作,后面跟着另一个字符,即不是最后一个字符的字符.我的代码是:
[0..(str.length - 2)].each do |index|
// do something with index
end
Run Code Online (Sandbox Code Playgroud)
代码出错了.[0..(str.length - 2)]
返回一个范围对象而不是可以迭代的实际范围.将a return index
放入上面的主体会导致单次迭代,输出类似于[0..7]
.为什么不迭代每个数字?
在Scala中,我需要执行满足以下要求的代码:
java.lang.Class
将a 传递给工厂方法.传递给此方法的类必须扩展抽象类Project
.我将实例化这个类,执行各种操作,然后返回它.
这是我的...
object ProjectFactory {
def create (clazz: java.lang.Class[T <: Project]): Project = {
val newProject = clazz.newInstance
/* snip */
newProject
}
}
abstract class Project
Run Code Online (Sandbox Code Playgroud)
......这是我得到的错误:
identifier expected but <: found
Run Code Online (Sandbox Code Playgroud)
我知道这只是一个语法问题 - 我如何表达这种逻辑?
这是我的函数代码:
char * increment_b_string(char * b_string)
{
char * ret_string = (char *) malloc(7);
ret_string = "001aaa";
if (*(b_string + 2) == '9')
{
*(ret_string +2) == '0';
if (*(b_string + 1) == '9')
{
*(ret_string +1) = '0';
*(ret_string) = *(b_string) + 1;
} else {
*(ret_string + 1) = *(b_string + 1) + 1;
}
} else {
*(ret_string + 2) = *(b_string + 2) + 1;
}
return ret_string;
}
Run Code Online (Sandbox Code Playgroud)
有关它为什么会失败的任何想法?
一般的想法是b_string将包含类似"123aaa"的值."aaa"部分并不重要,永远不会改变.只有前3个数字会.它们需要增加,就好像它们是整数一样.需要在开始时保留前导零.因此,如果输入为"004aaa",则输出应为"005aaa".这只需要达到"300aaa",因此我不会考虑更多的东西.这是一个学校的编程任务,因此问题非常人为.
谢谢.
编辑:我改变了我的功能.这就是现在的情况.
void increment_b_string(char * …
Run Code Online (Sandbox Code Playgroud) 这是相关代码:
Canvas.cpp
#ifndef CANVAS
#define CANVAS
#include "graphicsDatatypes.h"
class Canvas
{
private:
// Current and next points to draw to
struct cartesianPoint currentPoint, nextPoint;
public:
Canvas::Canvas() { numLinesDrawn = 0; };
Canvas::~Canvas();
struct cartesianPoint getCurrentPoint() { return currentPoint; };
void setCurrentPoint(int x, int y)
{
currentPoint.x = x;
currentPoint.y = y;
}
};
#endif
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "glut-3.7.6-bin\glut.h"
#include "Canvas.cpp"
// Window size
int winWidth, winHeight;
// User's drawing space - current maximum of 4000 lines
Canvas userDrawSpace();
void callbackMouse(int button, …
Run Code Online (Sandbox Code Playgroud) 我试图使用以下PHP类:
<?php
class Service {
public $code, $description;
public static $services = array(
"A" => "Shipping",
"B" => "Manufacturing",
"C" => "Legal",
"D" => "Accounts Receivable",
"E" => "Human Resources",
"F" => "Security",
"G" => "Executive",
"H" => "IT"
);
public function _construct( $c, $d) {
$this->code = $c;
$this->description = $d;
}
public static function getDescription( $c ){
return $services[$c];
}
public static function generateServiceList() {
$service_list[] = array();
foreach ($services as $k => $v ){
$service_list[] = new …
Run Code Online (Sandbox Code Playgroud) 几个月前,我使用此处的说明在Mac OSX 10.6上安装了ruby 1.9.1,并针对较新版本的ruby/rails/gem进行了修改.
http://hivelogic.com/articles/compiling-ruby-rubygems-and-rails-on-snow-leopard/
现在我需要一个项目,我需要使用旧版本的rails来开发一个站点,因此需要使用旧版本的Ruby.我已经成功安装了RVM,旧版本的gem和rails gem,但现在我对我的环境略感担忧.从技术上讲,我有3组Ruby安装程序 - OSX 10.6中包含的本机1.8.7,/ usr/local /中安装了我自己的1.9.1,RVM中安装了1.8.7.
我担心从长远来看这可能会导致奇怪的,难以诊断的错误.卸载我的1.9.1/usr/local和/或内置的1.8.7并将它们安装在RVM中是否值得?
我正在尝试执行以下PHP代码:
$path_hierarchy = // function that returns an array
return array_reduce(
$terms,
function($val1, $val2) use ($path_hierarchy) {
return $val1 || in_array($val2, $path_hierarchy);
}
);
Run Code Online (Sandbox Code Playgroud)
...但是我收到以下PHP错误:
PHP Parse error: syntax error, unexpected ')', expecting '{'
所以,我切换到以下语法:
$path_hierarchy = // function that returns an array
$callback = function($val1, $val2) use ($path_hierarchy) {
return $val1 || in_array($val2, $path_hierarchy);
};
return array_reduce(
$terms,
$callback
);
Run Code Online (Sandbox Code Playgroud)
......这很有效.我不能use
在匿名函数的上下文中使用关键字作为另一个函数的参数吗?