无法修改字符串的行

时间:2017-09-15 02:06:08

标签: ruby string

我有一个字符串file,我想删除它的一些行。当我将一些字符串函数用作deletereplacegsubclear等时,我将字符串变量设置为原始状态。

例如:

file
# => "#include <hidef.h> /* for EnableInterrupts macro */\r\n#include \"derivative.h\" /* include peripheral declarations */\r\n\r\n#ifdef __cplusplus\r\nextern \"C\"\r\n#endif\r\nvoid MCU_init(void); /* Device initialization function declaration */\r\n/*               >> 2 <- izquierda\r\n  Orden de leds 1 3 4 7\r\n                0 2 4 6\r\n                -> Derecha << 2*/\r\n\r\nunsigned short i;\r\nchar time;\r\nint estado=0;\r\nint cambio=0;\r\nint izquierda;\r\nint sec1; //1= secuencia 0-2-4-6-7-5-3-1\r\n          //0= secuencia 0-1-3-5-7-6-4-2\r\nvoid delay_ms(int time) {\r\n    \r\n    for (i = 0xFFFF; i > 0; i--)\r\n        ;\r\n    \r\n}\r\nvoid main(void) {\r\n    MCU_init(); /* call Device Initialization */\r\n    /* include your code here */\r\n    PTBDD = 0XFF; //Configuración del PORTB como salida\r\n    PTBD_PTBD0=1;\r\n    PTCDD_PTCDD0=0; //pulsador como entrada\r\n    PTCPE_PTCPE0=1; //pull-up\r\n    izquierda=0;\r\n    \r\n    for (;;) {\r\n        if (PTCD_PTCD0==0){\r\n            for (time=0x0F;i>0;i--); // lo aprete\r\n            while(!PTCD_PTCD0);\t//sigo apretando\r\n            for (time=0x0F;i>0;i--); //lo solte\r\n            apreto=0;\r\n            if (sec1==1) sec1=0;\r\n            else sec1=1;\r\n            switch (PTBD){\r\n                case 0x40:\r\n                    if (sec1==1){\r\n                        PTBD=0x80;\r\n                        izquierda=1;}\r\n                    else PTBD=PTBD >> 2;\r\n                break;\r\n                    \r\n                case 0x02:\r\n                    if (sec1==1){\r\n                        PTBD=0x01;\r\n                        izquierda=0;}\r\n                    else PTBD=PTBD << 2;\r\n                break;\r\n                \r\n                case 0x80:\r\n                    if (sec1==0){\r\n                        PTBD=0x40;\r\n                        derecha=0;}\r\n                    else PTBD=PTBD >> 2;\r\n                break;\r\n                \r\n                case 0x01:\r\n                    if (sec1==0){\r\n                        PTBD=0x02;\r\n                        derecha=1;}\r\n                    else PTBD=PTBD << 2;\r\n                break;\r\n                \r\n                default:\r\n                    if (sec1==1){\r\n                            if (izquierda==0)\r\n                                PTBD=PTBD << 2;\r\n                            else\r\n                                PTBD=PTBD >> 2;}\r\n                    else\r\n                        if (derecha==0)\r\n                            PTBD=PTBD >> 2;\r\n                        else\r\n                            PTBD=PTBD << 2;\r\n                break;\r\n            }\r\n            \r\n            \r\n        }\r\n    }\r\n}\r\n\r\n\r\n"
file.lines[15]
# => "int cambio=0;\r\n"
file.lines[15].replace("")
# => ""
file.lines[15]
# => "int cambio=0;\r\n"

如何保存已编辑字符串的状态?我也尝试使用其他变量但我得到了相同的结果。

1 个答案:

答案 0 :(得分:3)

当您调用.lines方法时。它实际上返回一个数组(行),它是原始字符串的副本(变量file中的内容)。因此,对file.lines所做的任何更改都不会回到file

因此,要更改字符串的第15行,您可能需要尝试:

lines = file.lines
lines[15].replace("")
file = lines.join("\r\n") + "\r\n" # this line does the work!