根据MDN JS Doc,该charAt方法接受integer并返回索引处的字符.和
如果您提供的索引超出范围,JavaScript将返回一个空字符串.
我发现它也是string一个参数,返回值很有趣.
示例代码:http://jsfiddle.net/yangchenyun/4m3ZW/
var s = 'hey, have fun here.'
>>undefined
s.charAt(2);
>>"y" //works correct
s.charAt('2');
>>"y" //This works too
s.charAt('a');
>>"h" //This is intriguing
Run Code Online (Sandbox Code Playgroud)
有没有人知道这是怎么发生的?
我有一个用utf-8编码存储的数据文件,我想将数据嵌入到erb模板中.数据文件在顶部用utf-8显式编码.但是在运行erb引擎但我遇到Encoding::CompatibilityError错误.
我认为Ruby中的默认编码是ASCII,erb模板也必须在ascii下编码.我明确地将它改为utf-8但没有好处.
这是数据文件:
# coding: utf-8
samples: [
{ name: '??', city: '??' }
]
Run Code Online (Sandbox Code Playgroud)
这是Erb模板:
<% # -*- coding: UTF-8 -*- %>
#...
<p><%= samples[:name] %></p>
Run Code Online (Sandbox Code Playgroud) 我遇到了一个非常棘手的案例:
class C {
// class method are implicit in strict mode by default
static method() { return this === undefined; }
}
C.method(); // => false
(0,C.method)(); // => true
Run Code Online (Sandbox Code Playgroud)
为什么要(0, C.method)改变this上述情况下的绑定?
我有样本行,我想#用=s 替换前导s.(前两行)但是string.gsub!(/^#+\w/, ""),我无法得到#我想要替换的数量.
在javascript中,我可以使用该replace方法的回调函数,但我怎么能归档这个Ruby?
##Command-line Tool
###Installment
This is a '#'.
Run Code Online (Sandbox Code Playgroud)
预期结果:
==Command-line Tool
===Installment
This is a '#'.
Run Code Online (Sandbox Code Playgroud) 我想插入一个div.row每三个块,以便将三个包装span在一起,用于以下haml片段.
但是这段代码插入<div class="row"></div>而不是包装.span4.
- data.apps.applications.each_with_index do |app, index|
- if index%3 == 0
.row # This is the line I want to insert
.span4
Run Code Online (Sandbox Code Playgroud)
我怎么能用haml或者在这种情况下,erb更合适?
在Ruby中,一切都是对象.我把它解释为一切都是从BasicObject祖先那里继承而来的.
但我发现美元变量($!,$1)没有祖先.
它们不响应ancestor定义的方法BasicObject.
如果我想java --jar sample.jar从命令行启动一个java可执行文件,我怎么能写bash脚本?以下不起作用
#!/usr/bin/java
--jar $HOME/tools/sample.jar
Run Code Online (Sandbox Code Playgroud) 我写张牌发电机,这里是解决方案,我想出了,但我发现它跨越多行和不好看,有没有其他的方式来建立这个卡吗?
deck = []
'23456789TJQKA'.each_char do |rank|
'SHDC'.each_char do |suit|
deck << rank + suit
end
end
Run Code Online (Sandbox Code Playgroud)