SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Noob Question: are Ruby variables non-global by default?

    I understand that in php...
    PHP Code:
    $num 
    ...there can only be one $num in a script...only the value can be changed.

    ...but in Ruby, you have to use the the $ symbol for global. does this mean that ruby variables are non-global by default whereas php variables are global by default? Or I must be a little confused.

    I'm in my first chapter of a ruby book.

    Thanks.

  2. #2
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Neither, are global be default. In ruby you would just use num = 3
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would happen if you defined a global variable then used the same variable name for a non-global variable? Would that cause conflict?

  4. #4
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Nope, it wont conflict.

    Though, as a warning using globals is generally a bad idea.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by The New Guy View Post
    Nope, it wont conflict.

    Though, as a warning using globals is generally a bad idea.
    Of course you mean: ...using the same variables twice (one global, one non-global) is a bad idea.

    ...right.

    Or are you ruling out globals all together?

  6. #6
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Globals all together.
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2005
    Posts
    38
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by The New Guy View Post
    Globals all together.
    Ok thanks. I'll remember that


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
  •