ruby中argv_Ruby中的插值

ruby中argv

Interpolation is about using expressions and values inside a string. For example, consider a variable named a which contains 4 and variable b containing 6. Now, we have to print 4 * 6 = 24

Interpolation是关于在字符串中使用表达式和值。 例如,假设一个名为a的变量包含4,而一个变量b包含6。现在,我们必须打印4 * 6 = 24

a * b = a*b. Will this work?
Interpolation example in Ruby

Nope. It returned an unexpected result. We need the values of a, b and a*b to be printed. Now, Interpolation comes into picture. Using Interpolation, we can include expression and values inside strings.

不。 它返回了意外的结果。 我们需要打印aba * b的值。 现在, Interpolation成为现实。 使用Interpolation ,我们可以在字符串中包含表达式和值。

To Interpolate the value of a, do the following: puts " #{a} "
Interpolation example in Ruby

It returned the value of a instead of "a" as a string. Likewise, we have to do the same for the variable b and the expression a*b.

以字符串形式返回a的而不是“ a” 。 同样,我们必须对变量b和表达式a * b进行相同的操作

Interpolation example in Ruby

Ruby evaluates the value of the variable or an expression given between the curly braces.

Ruby计算变量的值或花括号之间的表达式。

#{a*b}

#{a*b}

Ruby, evaluates the value of a*b and interpolates into string.

Ruby,计算a * b的值并将其内插到字符串中。

插值字符串: (Interpolating Strings:)

Consider a variable with a value.

考虑具有值的变量。

person = "Sachin Tendulkar";

person = "Sachin Tendulkar";

Now, we have to display "I love Sachin Tendulkar".

现在,我们必须显示“我爱Sachin Tendulkar”。

Interpolation example on Strings in Ruby

We've expected "I love Sachin Tendulkar" but it displayed "I love person". Now, let's interpolate the variable person.

我们期望“我爱Sachin Tendulkar”,但显示“我爱人” 。 现在,让我们对变量person插值。

Interpolation example on Strings in Ruby

翻译自: https://www.studytonight.com/ruby/interpolation-in-ruby

ruby中argv

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux内核,也有一个函数叫做`argv_split`,它用于将用户空间传递给内核的命令行参数字符串分割成多个子字符串。在内核,这个函数通常用于解析用户空间传递的命令行参数,并将它们作为参数传递给系统调用或驱动程序。 `argv_split`函数的实现与用户空间的实现略有不同,因为内核不能使用标准库函数。下面是一个简化版的内核实现: ```c #include <linux/string.h> #define MAX_ARGS 32 static char *argv[MAX_ARGS]; int argv_split(char *str, char **argbuf, int argbuf_len) { int argc = 0; char *s = str; char *arg = NULL; int arg_len = 0; int in_quotes = 0; int i; while (*s) { if (argc == argbuf_len) return -1; if (*s == '"') { in_quotes = !in_quotes; } else if (*s == ' ' && !in_quotes) { arg = kmalloc(arg_len + 1, GFP_KERNEL); if (!arg) return -1; memcpy(arg, str, arg_len); arg[arg_len] = '\0'; argbuf[argc++] = arg; str = s + 1; arg_len = 0; } else { arg_len++; } s++; } if (arg_len) { arg = kmalloc(arg_len + 1, GFP_KERNEL); if (!arg) return -1; memcpy(arg, str, arg_len); arg[arg_len] = '\0'; argbuf[argc++] = arg; } for (i = 0; i < argc; i++) argv[i] = argbuf[i]; argv[argc] = NULL; return argc; } ``` 该函数接收一个字符串`str`,以及一个指向存储子字符串的缓冲区的指针`argbuf`和缓冲区的长度`argbuf_len`。函数会将`str`的子字符串分割成多个部分,并将它们存储在`argbuf`。如果`argbuf`不够大,函数将返回-1。 注意,由于该函数运行在内核态,因此不能使用标准库函数。在上面的实现,我们使用了Linux内核提供的`kmalloc`函数来分配内存。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值