说我想要puts字母表.所以我可以这样做:
alphabet = ('a'..'z')
alphabet.map do |a|
puts a
end
Run Code Online (Sandbox Code Playgroud)
我现在要做的是排除vowels.
alphabet = ('a'..'z')
vowels = ['a','e','i','o','u']
alphabet.map do |a|
puts a unless a == vowels
end
Run Code Online (Sandbox Code Playgroud)
我试图避免这种情况:
alphabet = ('a'..'z')
alphabet.map do |a|
puts a unless a == 'a'
puts a unless a == 'e'
puts a unless a == 'i'
puts a unless a == 'o'
puts a unless a == 'u'
end
Run Code Online (Sandbox Code Playgroud)
我如何在语法上实现第二个示例,以便它正常工作?
我试图了解String#capitalize!内部如何运作.我可以创建一个哈希.给定字符串foo = "the",foo[0]是"t",查找lower_case "t",并将其与大写"T"值匹配.实际上,Ruby源代码显示:
static VALUE
rb_str_capitalize_bang(VALUE str)
{
rb_encoding *enc;
char *s, *send;
int modify = 0;
unsigned int c;
int n;
str_modify_keep_cr(str);
enc = STR_ENC_GET(str);
rb_str_check_dummy_enc(enc);
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return Qnil;
s = RSTRING_PTR(str); send = RSTRING_END(str);
c = rb_enc_codepoint_len(s, send, &n, enc);
if (rb_enc_islower(c, enc)) {
rb_enc_mbcput(rb_enc_toupper(c, enc), s, enc);
modify = 1;
}
s += n;
while (s < send) { …Run Code Online (Sandbox Code Playgroud) 我需要得到所有可能的数字组合,denom_arr其中相等amt.
denom_arr = [4,3,1]
amt = 10
Run Code Online (Sandbox Code Playgroud)
这种情况会产生:
[4, 4, 1, 1][3, 3, 3, 1][1, 1, 1, 1, 1, 1, 1, 1, 1, 1][4, 3, 1, 1, 1][4, 3, 3]问题是我写的代码正在破坏1-3,我不知道如何让它循环在同一个索引上来获取案例4-6+
set, sets = [], []
i = 0
loop do
i = 0 if denom_arr[i].nil?
loop do
set << denom_arr[i]
break if set.inject(:+) > amt
end
set.pop if set.inject(:+) > amt
if set.inject(:+) == amt
sets << …Run Code Online (Sandbox Code Playgroud) 简单的正则表达式无法正常工作。例如first_name = "a11"工作正常。为什么我的格式正则表达式无法正确验证?
validates :first_name, presence: { message: "Name cannot be blank." },
format: { with: /[a-z]/i, message: "Name must only contain letters." },
length: { minimum: 2, message: "Name must be at least 2 letters." }
Run Code Online (Sandbox Code Playgroud) 我想知道Bootstrap是否具有内置JS功能,可根据设备屏幕大小显示或隐藏行?可折叠的列不能满足我的需求.
例如(伪代码)
if(screensize > 768px) {
<div class="row">
<div class="col-md-8">
<section>
<ul>
<li> <%= image_tag 'section_icons/cheese.png', class: 'section_img' %> Cheese </li>
<li> <%= image_tag 'section_icons/wine.png', class: 'section_img' %> Wine </li>
</ul>
</section>
</div>
<div class="col-md-4">
<div> other column content </div>
</div>
</div>
} else {
<div class="row">
<div class="col-xs-12">
<section>
<ul>
<li> <%= image_tag 'section_icons/cheese.png', class: 'section_img' %> Cheese </li>
<li> <%= image_tag 'section_icons/wine.png', class: 'section_img' %> Wine </li>
</ul>
</section>
<div> other column content </div>
</div> …Run Code Online (Sandbox Code Playgroud) 这是我的模型 - 我想分配行并在视图页面上回显,例如<?php echo $product?>
class Model_products extends CI_Model {
function printer()
{
$id = $this->uri->segment(3, 0);
$q = $this->db->get_where('printer', array('id' => $id));
if ($q->num_rows == 1)
{
foreach ($q->result() as $row)
{
echo $row->name; # THIS WORKS
$data['product'] = $row->name; # THIS DOES NOT WORK
$data['desc'] = $row->description;
$data['size'] = $row->size;
$data['options'] = $row->options;
$data['more'] = $row->more_info;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器
public function products()
{
$this->load->model('Model_products');
$this->Model_products->printer();
$this->load->view('products', $data);
$this->load->view('pricing');
}
Run Code Online (Sandbox Code Playgroud)
我的看法是
<h1>
<?php echo $product; ?> …Run Code Online (Sandbox Code Playgroud) class Human
@core = "heart"
def cardiovascular
arr = ['heart','blood','lungs']
core = @core
end
end
Run Code Online (Sandbox Code Playgroud)
是我能够@core直接使用此方法访问的唯一方法:
Human.instance_variable_get(:@core) #=> "heart"
Run Code Online (Sandbox Code Playgroud)
我的理解是,一个实例变量是从任何位置访问范围中的类.
我可以通过以下方式访问该方法:Human.new.cardiovascular我期望返回"heart"但是我得到的回报是nil
Human.core还是Human.new.core?Human.new.cardiovascular回归nil?(不应该核心== @核心?)更新
放入@core初始化块后,我在IRB中看到以下输出:
Human.new
=> #<Human:0x2f1f030 @core="heart">
Run Code Online (Sandbox Code Playgroud)
这是有意义的,因为它现在可用于整个类,但如何访问初始化块中的特定实例变量?意思是,我如何得到:在这种情况下@core不调用cardiovascular方法?
我有一些基于所选表单输入动态生成的按钮:
$.each(fields, function (i, field) {
var field_id = $('[name=' + field.name + ']').closest("fieldset").attr('id');
$("#results").append('<button id="jumpToThisStep" data-id="'+field_id.replace('q','')+'">'+field.value+ ' ' +'</button>');
});
Run Code Online (Sandbox Code Playgroud)
}
在我的doc.ready功能中,我有以下内容:
$('#jumpToThisStep').click(function() {
var jump_to = $(this).data('id');
showStep(jump_to);
});
Run Code Online (Sandbox Code Playgroud)
HTML:
<button id="jumpToThisStep" data-id="0"> ... </button>
<button id="jumpToThisStep" data-id="1"> ... </button>
<button id="jumpToThisStep" data-id="2"> ... </button>
Run Code Online (Sandbox Code Playgroud)
在检查元素时,它们都具有适当的data-id绑定.但是唯一一个发射的是第一个.是什么阻止他人预先形成他们的.click?
我想添加一个one?方法,Array#size以便我可以说:
class Array
def one?
self.size == 1
end
end
[1].size.one?
#=> true
[1,2].size.one?
#=> false
Run Code Online (Sandbox Code Playgroud) 如何inputs通过输入类型(text | checkbox | radio)匹配页面上的所有表单到数组?
就像js这样,这将给我所有的inputs
var inputs = document.getElementsByTagName( 'input' );
Run Code Online (Sandbox Code Playgroud)
我有jQuery加载,我尝试:根据文档(http://api.jquery.com/text-selector/)
$( "<input>" ).is( ":text" );
$( "<input>" ).is( "[type=text]" );
Run Code Online (Sandbox Code Playgroud)
TypeError: undefined is not a function
message: "undefined is not a function"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error
Run Code Online (Sandbox Code Playgroud)
我试图获取text输入的页面是在iFrame中,整个交互在webkit中运行.
需要在我的主页面上使用jQuery在iFrame页面上获取输入.
我不明白为什么switch-case statement要求你break在每个之后明确插入一个case.一旦case制成,它的重点不是停止吗?
有人可以给我一个case找到a的情况,true但由于某种原因[ 你在这里插入原因 ]你仍然需要代码块来执行.
这是一个虚荣心 PHP switch-case statement
switch ($error) {
case 'empty':
$msg = 'Field cannot be empty.';
break;
case 'invalid':
$msg = "Field may only contain number.";
break;
}
Run Code Online (Sandbox Code Playgroud)
这是一个invaild PHP switch-case statement
switch ($error) {
case 'empty':
$msg = 'Field cannot be empty.';
case 'invalid':
$msg = "Field may only contain number.";
}
Run Code Online (Sandbox Code Playgroud)
那么这break无用还是在某些情况下有用?