Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 12th, 2012, 11:20 PM
tescrin tescrin is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 14 tescrin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 24 m 32 sec
Reputation Power: 0
Quick Question from a newb

I'm writing a piece of AI code as a means to learn Ruby. I'm getting confused on some syntactical choices that don't seem to make a differece (yet.)

So here are a flurry of questions:
-When defining a global variable; do you use the '@' sign? Why or why not? I.E. somevar vs. @somevar
It seems I don't need to, but it feels wrong to have variables that have different symbols in front of them
-When defining class variables; do you use '@', '@@', or just the name? Why? It seems '@' is the appropriate way to go but various websites show people do it different ways.
-Would this be the proper way to copy identity's value into a variable? Again, it seems to work in the code but it may be inefficient, deprecated syntax or something else.
Code:
class Piece
   def initialize
     @value=0
   def value
     @value
   end
end
p = piece.new.value


Thank you for helping a newb on a crash course

Reply With Quote
  #2  
Old April 24th, 2012, 01:22 PM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 21 m 30 sec
Reputation Power: 38
Quote:
Originally Posted by tescrin
So here are a flurry of questions:
-When defining a global variable; do you use the '@' sign? Why or why not? I.E. somevar vs. @somevar


Global variables are prefixed with a dollar sign. So $foo would be a global variable. @foo is an instance variable and foo without a prefix is a local variable. You use a local variable if you only need the variable within the current scope. You use an instance variable when you need the variable to be accessible from anywhere within the current object. You use global variables when... Well, preferably you don't use global variables.

Quote:
-When defining class variables; do you use '@', '@@', or just the name? Why? It seems '@' is the appropriate way to go but various websites show people do it different ways.


I've explained when to use instance variables above. Variables that start with @@ are class variables (also known as static variables in other languages). You use class variables when you need the variable to have the same value for all objects of the same class.

Quote:
-Would this be the proper way to copy identity's value into a variable? Again, it seems to work in the code but it may be inefficient, deprecated syntax or something else.
Code:
class Piece
   def initialize
     @value=0
   def value
     @value
   end
end
p = piece.new.value



I don't know what it means to "copy identity's value into a variable", but the code you've shown doesn't do anything useful at all. First of all it contains a syntax error (missing "end" in initialize). Then even if you fix that the class really doesn't do anything. If you create it, you set @value to 0 and it will stay 0 as long as the object exists. Then you can call value to get the value of @value. So basically you can create Piece objects and make them tell you that their value is 0.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Quick Question from a newb

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap