我有一个程序,我试图理解,但有一个我不明白的缩写代码或代码.代码是
double xDistance = x1 - x2 >= 0 ? x1 - x2 : x2 - x1;
double yDistance = y1 - y2 >= 0 ? y1 - y2 : y2 - y1;
Run Code Online (Sandbox Code Playgroud)
我在编程书籍中读到了?:是if ... then的缩写形式但是我把这些单词放在哪里,因为每次我插入或替换一些代码我都会遇到语法错误.
还有不止一种方法来编写以下代码行
(xDistance <= (w1 + w2) / 2 && yDistance <= (h1 + h2) / 2)
Run Code Online (Sandbox Code Playgroud)
最好替换&&运算符
提前感谢您提供的任何帮助.
前言(因为这是我的第一个铁杆帖子)我喜欢铁轨,但目前它让我抓狂!两年前我玩过rails 2.3.现在我开始使用rails 3.1.一切都只是为了好玩.Rails是我在Web开发中见过的最好和最专业的.但是......由于一行代码具有如此强大的功能,因此很难学习它!铁路魔术很难获得:-)
问题 我有一个嵌套的模型和路径.一页有很多篇文章.我渲染pages/show.html.haml并在页面中列出所有文章.每篇文章都应该在"new","edit"和"destroy"之下.
render @articles
Run Code Online (Sandbox Code Playgroud)
这是我的观点/ articles/_article.html.haml
%li.article_list{:id=>"a#{article.id}"}
.article
.article_head
%h2
=article.title:class => "icon"), '#'
.clear
.article_content
= raw parse_content(article.content)
-if admin?
#article_menu
%ul
%li= link_to icon_new + 'New...', new_page_article_path(@page)
%li=# link_to icon_edit + 'Edit', edit_page_article_path(WHAT SHALL BE IN HERE???)
%li= link_to icon_destroy + 'Destroy', [article.page, article], :confirm => "#{article.title}\n\nAre you sure?", :method => :delete
Run Code Online (Sandbox Code Playgroud)
我的问题是这一行
%li=# link_to icon_edit + 'Edit', edit_page_article_path(WHAT SHALL BE IN HERE???)
Run Code Online (Sandbox Code Playgroud)
我想编辑文章,但我不知道怎么做!我试过几个小时的谷歌!
一般来说,我一直在关注这个很好的指南:http://guides.rubyonrails.org/getting_started.html#associating-models
附加信息
车型/ article.rb
class Article < …Run Code Online (Sandbox Code Playgroud) 我正在尝试将this其中的ID设置为相同的字符串,但是在课堂上.
例如:
$("#FirstBox").click(function () {
$('.FirstBox').trigger('click');
});
Run Code Online (Sandbox Code Playgroud)
而不是键入.FirstBox,我如何设置一个变量,以便它$(this)在课堂上?
我有两个版本的代码.在Method()中抛出的第一个版本类型的异常 - NullPointerException在第二个版本中 - Exception.但是第一个版本将编译但第二个版本将无法编译.为什么会这样?
public class Demo
{
static void Method()
{
try
{
throw new NullPointerException("error");
}
catch(Exception ex)
{
throw ex;
}
}
public static void main(String argv[])
{
try
{
Method();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是第二个版本.
public class Demo
{
static void Method()
{
try
{
throw new Exception("error");
}
catch(Exception ex)
{
throw ex;
}
}
public static void main(String argv[])
{
try
{
Method();
}
catch(Exception ex)
{ …Run Code Online (Sandbox Code Playgroud) RoR中的类是否在创建该类的新对象时自动启动第一个方法?
class User
attr_accessor :name, :email
def initialize(attributes = {})
@name = attributes[:name]
@email = attributes[:email]
end
def formatted_email
"#{@name} <#{@email}>"
end
end
Run Code Online (Sandbox Code Playgroud)
假设我像这样创建一个新用户
connor = User.new(name: "Connor B", email: "CB@example.com")
Run Code Online (Sandbox Code Playgroud)
它如何知道自动启动第一个方法,但第二个方法只在被调用时才有效?