我使用的是 MacOS,并且已经安装了 VSCode、Ruby、rubocop、Ruby Solargraph 及其 gem 依赖项。
完成此操作后,我现在如何在不使用 VSCode 中的终端面板的情况下执行 Ruby 脚本?
我试图使用循环迭代以下字符串for
:
>>> for a,b,c in "cat"
print(a,b,c)
Run Code Online (Sandbox Code Playgroud)
现在我打算做的是在一个物理线上单独打印字符串中的每个字符,而不是我收到错误.我知道通过将字符串包含在list运算符中可以很容易地解决这个问题[]
:
>>> for a,b,c in ["cat"]
print(a,b,c)
c a t
Run Code Online (Sandbox Code Playgroud)
但有人可以解释为什么会这样吗?
在Python中,如果使用bool
函数,我可以确定表达式求值的布尔值。例如:
bool(1)
#=> True
Run Code Online (Sandbox Code Playgroud)
Ruby中是否存在这样的构造?我似乎找不到找到这样做的证据。我目前必须使用等效测试来确定布尔值,并且想知道是否有更方便的方法来执行此操作。
Enumerator
类的文档页面上列出了以下方法:
::new
#each
#each_with_index
#each_with_object
#feed
#inspect
#next
#next_values
#peek
#peek_values
#rewind
#size
#with_index
#with_object
但是,当我在irb
收到以下内容时false
:
>> Enumerator.methods.include?(:next)
false
Run Code Online (Sandbox Code Playgroud)
这是因为即使该方法可以用于此类的实例,但该方法并未在类本身上显式定义,即它是继承的?考虑到这一点,我还检查了Enumerator
超类,发现它们也不包含此方法:
>> Enumerator.superclass.methods.include?(:next)
false
>> Enumerator.superclass.superclass.methods.include?(:next)
false
Run Code Online (Sandbox Code Playgroud)
我确定我忽略了一些非常基本的东西。
我正在使用Python 3,我希望看到原始/真实表示与使用单引号和双引号的转义序列的字符串表示之间的区别,因此我创建了以下脚本:
raw = "%r" % "\'\""
str = "%s" % "\'\""
print(raw)
print(str)
Run Code Online (Sandbox Code Playgroud)
print(str)返回(按预期):
'"
Run Code Online (Sandbox Code Playgroud)
现在我希望print(raw)返回:
'\'\"'
Run Code Online (Sandbox Code Playgroud)
然而它返回:
'\'"'
Run Code Online (Sandbox Code Playgroud)
为什么print(raw)语句中只有一个反斜杠,不应该有两个,因为这会反映我已经解析为格式化字符串的值?对不起这个愚蠢的问题我很抱歉
我是Python和编程的绝对初学者,我刚刚接触过函数.
我在下面定义了两个简单的函数:
def output1():
print "Hello, world!"
def output2():
print "Hello, there!"
output1()
output2()
Run Code Online (Sandbox Code Playgroud)
将上面的内容保存到一个名为function.py的脚本后,我使用windows power shell运行脚本,并按照您的预期打印以下内容:
Hello, world!
Hello, there!
Run Code Online (Sandbox Code Playgroud)
但是当我将脚本修改为:
def output1():
print "Hello, world!"
def output2():
print "Hello, there!"
print output1()
print output2()
Run Code Online (Sandbox Code Playgroud)
它打印:
Hello, world!
None
Hello, there!
None
Run Code Online (Sandbox Code Playgroud)
出于好奇,当我将output1和output2作为print前缀时,为什么会这样做呢?
python ×4
ruby ×3
python-3.x ×2
for-loop ×1
function ×1
iteration ×1
methods ×1
python-2.7 ×1
string ×1