我试图找出insertRuby中的函数功能.
我已经咨询了谷歌和ruby-doc.com,但解释不足以描述这个(看似)神秘的功能.
这让我很困惑:
a = %w{a b c d}
puts a.insert(4, 5) output = a,b,c,d,5
Run Code Online (Sandbox Code Playgroud)
提出的第一个问题是,为什么没有插入4?
puts a.insert(2,2,6)
Run Code Online (Sandbox Code Playgroud)
输出是:
a
b
2
6
c
d
Run Code Online (Sandbox Code Playgroud)
提出的两个问题是
特定的代码行不会导致输出,并且IRB退出>>提示.我不是要退出IRB,而是退出到导致>>提示消失的代码行之前的状态.
>> stop_words = %w {the a and if}
>> stop_words.each{|x| stop_words << x.capitalize}
quit
quit
quit
Run Code Online (Sandbox Code Playgroud)
stop_words来进行实验.任何阐明/洞察这里发生了什么是值得赞赏的.我做了各种各样的调试,并参考了很多来源,但我自己也无法解决这个问题.我刚刚开始在PHP中使用OOP而且我正在尝试写一个扑克手牌经销商.我尝试使用递归函数和循环以及if else算法来避免在pokerHand数组中创建重复项.我对这个代码应该如何工作的想法是,当一个新的pokerHand对象被创建时,__construct函数将被自动调用(这是我知道使用构造函数的唯一原因,但我认为还有更多它).然后在do while循环中,代码初始化一个变量$ rand,这是一个介于1和52之间的rand数,并且in_array检查是否已经在数组中输入了"card".如果卡已经发出,__construct函数应该调用自己生成另一张卡,直到新卡出现之后才将其存储在阵列中.
我刚刚开始作为一名程序员,对我的代码的任何反馈对我来说都是无法估量的,我提前感谢你.
<?php
class pokerHand {
private $_pokerHand = array();
private $_counter = 0;
public function __construct (){
do{
if (in_array ($rand = rand(1,52), $this->_pokerHand) ){
return public function __construct();
} else {
$this->_pokerHand[] = $rand;
$this->counter++;
}
} while ($this->_counter < 5)
public function showHand(){
print_r ($this->_pokerHand);
}
}
$obj = new pokerHand();
$obj => showHand();
?>
Run Code Online (Sandbox Code Playgroud) 如果我在 30 天试用期到期之前卸载 Rubymine,以后还可以再次使用该试用版吗?我只使用了一天,担心如果卸载它,我将无法免费试用它。
我有一个trip_cost计算假期总费用的功能.如果我想打印函数的结果我可以这样做而没有问题:
print trip_cost(city, days, spending_money)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试使用字符串编写更易于理解,用户友好的版本,我会得到一个 Syntax Error: Invalid Syntax
print "Your total trip cost is: " trip_cost(city, days, spending_money)
Run Code Online (Sandbox Code Playgroud)
怎样才能解决这个问题?
我试图将字符串中的第一个字符更改为大写.我这样接近它:
word = "dalmatian"
word[0] = word[0].upper()
print word
Run Code Online (Sandbox Code Playgroud)
但是我产生了这个错误
Traceback (most recent call last):
File "/Users/Tom/Documents/coolone.py", line 3, in <module>
word[0] = word[0].upper()
TypeError: 'str' object does not support item assignment
Run Code Online (Sandbox Code Playgroud)
有没有解决的办法?