我正在编写一个像这样的Ruby循环:
iterations = 10
until iterations == 0
unless iterations == 1
puts "#{iterations} iterations until done"
else
puts "#{iterations} iteration until done"
end
iterations -= 1
end
puts "Done"
Run Code Online (Sandbox Code Playgroud)
我想知道是否有更多"Ruby方式"来优化/编写此代码.有人有什么想法吗?
我有这个代码:
class Company < ActiveRecord::Base
has_many :users, :through => :company_users do
def regular
where('regular_user = ?', true)
end
def employee
where('regular_user = ?', false)
end
end
Run Code Online (Sandbox Code Playgroud)
我想知道另一种写这种方式,或者这是最有效的方法.我在考虑用户模型中的范围.有任何想法吗?
我试图理解为什么脚本可以使用#!/bin/bash
但不是#!/bin/sh
.我运行Cygwin和都sh.exe
和bash.exe
似乎是相同的(相同的文件大小).
$ cat 1.sh
#!/bin/sh
while read line; do
echo ${line:0:9}
done < <(help | head -5)
$ ./1.sh
./1.sh: line 4: syntax error near unexpected token `<'
./1.sh: line 4: `done < <(help | head -5)'
$ cat 2.sh
#!/bin/bash
while read line; do
echo ${line:0:9}
done < <(help | head -5)
$ ./2.sh
GNU bash,
These she
Type `hel
Use `info
Use `man
Run Code Online (Sandbox Code Playgroud) 我想只打印使用cURL检索的网页的HTTP状态代码.是否可以使用AWK单线程进行此操作?
使用Rails 3,但我猜并不重要.我把它放在模板助手中.
badge-warning
当我为我付出的任何价值时,它始终是最后一种情况rating
,例如rating_badge(4)
:
def rating_badge(rating)
case rating
when rating > 3
'badge-success'
when rating < 3
'badge-important'
else
'badge-warning'
end
end
Run Code Online (Sandbox Code Playgroud)
什么地方出了错?
我写了一个小的bash程序,需要读取一个带有名字的文件input
.我希望脚本打印消息file not found
,如果找不到文件则退出或自行终止.
在这个例子中:
#!/usr/bin/perl
$a = 4; # Is $a a local variable or a global variable?
print $a, "\n";
Run Code Online (Sandbox Code Playgroud)
$ a有什么样的范围?
我是Ruby的新手.我正在做Michael Hartl的Ruby on Rails教程,并在用户的模型中使用以下代码:
before_save { self.email = email.downcase }
Run Code Online (Sandbox Code Playgroud)
在这种情况下,可以接受写:
before_save { self.email.downcase! }
Run Code Online (Sandbox Code Playgroud)
或者由于某种原因这是否有缺陷?如果是的话,你能给我一个快速解释原因吗?
我无法理解花括号内的部分.
Array.new(10) { |e| e = e * 2 }
# => [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
Run Code Online (Sandbox Code Playgroud)
我知道创建了一个包含十个值的新数组,但下半部分是做什么的?
看看这段代码:
def hello
p "Hey!"
end
p hello
Run Code Online (Sandbox Code Playgroud)
输出将是:
"Hey!"
"Hey!"
=> "Hey!"
Run Code Online (Sandbox Code Playgroud)
所以这是我的结论:put本身返回将在Ruby代码中输出的文本,否则它不会打印"嘿!" 再次.打印字符串时发生了什么?如果puts不直接将它发送到标准输出,谁负责它以及如何?
我需要立即找出并输出模式之前的 10 行和模式之后的 20 行。我究竟做错了什么?我没有得到正确的结果。
grep -B 10 "next" file1.txt | grep -A 20 "next" file1.txt
Run Code Online (Sandbox Code Playgroud) 我想迭代这些数据来提取id的值:
[ {:id => 3, :quantity => 5 }, { :id => 4, :quantity => 3 } ]
Run Code Online (Sandbox Code Playgroud) 我需要帮助从一个类到另一个类读取变量.我已经对此有了一些答案,但没有成功.
PP.h:
@interface PP : UIViewController
{
@public int fNum;
}
@property (readwrite, nonatomic) int fNum;
- (IBAction)setSomeNum;
@end
Run Code Online (Sandbox Code Playgroud)
PP.m:
@synthesize fNum;
- (IBAction)setSomeNum
{
fNum = 69;
NSLog(@"Set Num Activated %d",fNum); //OK
}
Run Code Online (Sandbox Code Playgroud)
TryView.m:
#import "TryView.h"
#import "PP.h"
@interface TryView ()
@end
@implementation TryView
- (void)viewDidLoad
{
[super viewDidLoad];
PP *obj ;
int x = obj.fNum;
NSLog(@"Happines %d",x); //Prints 0
}
@end
Run Code Online (Sandbox Code Playgroud)
有什么不对,为什么打印0?
ruby ×6
bash ×2
scope ×2
arrays ×1
awk ×1
before-save ×1
constructor ×1
curl ×1
grep ×1
hook ×1
http-headers ×1
idioms ×1
if-statement ×1
io ×1
ios ×1
iteration ×1
linux ×1
loops ×1
objective-c ×1
perl ×1
ruby-block ×1
sh ×1
shell ×1
string ×1