SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Quick questions

  1. #1
    Strictly Professional. ralxz's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick questions

    Hey guys. I'm new to Ruby and Rails so I have a few quick questions, if you don't mind answering.

    1. What's the difference of <% %> and <% -%>? I read that the one with the - makes it so ruby doesn't create a newline after, but does that mean if you place a <% %> in a rhtml file it will create a line as if you would do a <br />?

    2. I'm pretty sure this is extremely important, hehe. What does it mean when you do this? @page_title || "Index". Is this the same as @page_title = "Index"?

    3. I know if you add a ! it means it is destructive, and it's basicly just hinting to future developers (or yourself) that it does something destructive. Does ? do the same thing? (nothing)

    Thanks a bunch,
    Ryan
    Last edited by ralxz; Mar 16, 2006 at 19:10. Reason: Added q 3
    Open for design work. PM or through email.

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ralxz
    Hey guys. I'm new to Ruby and Rails so I have a few quick questions, if you don't mind answering.

    1. What's the difference of <% %> and <% -%>? I read that the one with the - makes it so ruby doesn't create a newline after, but does that mean if you place a <% %> in a rhtml file it will create a line as if you would do a <br />?
    It doesn't create a <br/>, it just creates a newline in the source code, which will show up as a single space in the rendered version. The -%> is nice if you don't want a bunch of whitespace while you're looking at the generated HTML output.
    Quote Originally Posted by ralxz
    2. I'm pretty sure this is extremely important, hehe. What does it mean when you do this? @page_title || "Index". Is this the same as @page_title = "Index"?
    I think that means "use @page_title if it has a value, otherwise use 'Index'".

    Consider something similar in PHP:
    PHP Code:
    $page_title = (isset($page_title)) ? $page_title 'Index'

  3. #3
    Strictly Professional. ralxz's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    It doesn't create a <br/>, it just creates a newline in the source code, which will show up as a single space in the rendered version. The -%> is nice if you don't want a bunch of whitespace while you're looking at the generated HTML output.

    I think that means "use @page_title if it has a value, otherwise use 'Index'".

    Consider something similar in PHP:
    PHP Code:
    $page_title = (isset($page_title)) ? $page_title 'Index'
    Ahh, alright. Thanks a lot.
    Open for design work. PM or through email.

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And ? is used on the end of a method name to indicate its a boolean method, i.e. @user.logged_in?

  5. #5
    Strictly Professional. ralxz's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    And ? is used on the end of a method name to indicate its a boolean method, i.e. @user.logged_in?
    Alright, so it doesnt "actually" do anything?
    Open for design work. PM or through email.

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ralxz
    Alright, so it doesnt "actually" do anything?
    Nor does the !. It just implies that the method makes a change directly on the object, instead of returning a modified copy (such as gsub! or chomp!). It's a suggested naming guideline nothing else.

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, the correct translation of @page_title || "Index" is:

    Code:
    (isset($this->page_title) && $this->page_title) ? $this->page_title : 'Index';
    || does not do any assignments. If the value before || is true (or any other true value), it returns that value. If it is false, it returns the second value:

    34 || 21 => 34
    false || 21 => 21
    false || false => false
    false || nil => nil
    nil || false => false

    You can pronounce it as "or", and you can indeed write it as or:

    34 or 21 => 34

    There is a ||= too:

    a_variable ||= b

    Which is the same as:

    a_variable = a_variable || b

    pronounce "set a_variable to b if a_variable is not set" (or is false or nil)

    Another tip: you can chain ||:

    a_variable = foo || bar || baz


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •