What is .new for? And some Questions about Variables

● ARCHIVED · READ-ONLY
Started by Chefchen 6 posts View original ↗
  1. Maybe it is because of the english tutorial i used to learn ruby a bit while im german, but for some reason i did'nt really understand what .new does. Why do you need it?

    So i was not sure about @ either so i googled it up and found out that @variable are for Classes only while local variables are for method's.

    And variables with a $ in front of it is a global variable which can be accessed from anywhere.

    But couldnt i just use locals instead of instance variable? Shouldnt they work both just fine?

    Edit:
    Oh and btw, what does .set_handler do?

    And yea, sorry for my bad english :)
  2. Use a local variable when you do not want the value to be stored inside the object.  For example, if you perform a loop, the loop variable can be local, because you don't care about the value once the loop is finished.  These have no special markers in front, like loop=startValue

    Use an instance variable when you want each object of that type to remember the value.  For example, if you have a Point with an X and Y coordinate, each Point should store X and Y as instance variables, so all Point methods can refer to the proper X and Y values for that object.  These have a single @ mark in front, like @x=3

    Use a class variable when you want ALL objects of a type to share a value.  For example, if you have a counter that indicates how many Points you have, you could store that counter as a class variable in the Point class.  These are defined with 2 @ marks in front, like @@point_counter=0

    Use a global variable when you want OTHER objects to be able to read and modify a value.  For example, if you have a set of Points on a Canvas, and want only one Canvas to exist, you could use a global variable.  These are defined with 1 $ mark in front, like $single_canvas=Canvas.new()

    The .new method creates an instance of a particular object, by calling that object's initialize method with the parameters after the new, like:

    $single_canvas=Canvas.new(640, 480)

    This would call this:

    class Canvas

    # This method is called

    def initialize(width, height)

    end

    end

    I hope this helps.
  3. Oh and btw, what does .set_handler do?
    It sets the method that would be run when a certain "command" is "pressed". It's used for Window_Selectable and it's child classes.
  4. Local variables are used to temporarily store data during an operation. They are unique for every method call, so even if you use the same local variable names within multiple method definitions or process the same method several times at once, the calls will not interfere with each other.

    Instance variables are unique for every created instance of the class. They can be best compared to an events self switches: modifying a self switch within an event will only affect the event itself, but none of the other events that use the same self switch.

    In ruby, every object you create has its own set of instance variables. If you access an instance variable within a method, the "owner" of the variable will be the object you call the method upon:

    Spoiler
    class Lamp   # The initialize method is called when a new object is  # created by calling  Lamp.new  # By default, every lamp is turned off.  def initialize    @powered = false  end   # Turn the selected lamp on:  def turn_on    @powered = true  end  # Print info about the power status of the selected lamp:  def info    print "Lamp #{ self.object_id } is turned #{ @powered ? 'ON' : 'OFF' }!"  endend # --- TEST: ---lamp1 = Lamp.new  # Create an instance of Lamplamp2 = Lamp.new  # Create another instance of Lamp lamp2.turn_on  # Turn the second lamp on.lamp1.info# => "Lamp 20200090 is turned OFF!"lamp2.info# => "Lamp 20200080 is turned ON!"

    Class variables are as such similar to global variables, but with some restrictions:
    - a class variable can only be accessed directly within the class itself (or one of their subclasses).
    - trying to read an undefined class variable will result in an error (may reveal typing errors)
    - independent classes can use the same class variable names without interfering with each other.
     
  5. Just for fun, let me explain it my way :D

    .new creates a new "instance" of the Object.

    Ruby is the purest (modern) Object-oriented programming language. You work with objects all the time. A party Actor is an object, who has it's own HP and Mana.

    Let's say there is a class called Table. This defines how a Table should behave or look like.

    If you do: Table.new - then voilá! You just created a Table. In programming you can create as many as you want immediately :D

    Now, Table alone is the blueprint to create a "new" table, but it's not a table on itself. Let me ask you this: Do you have breakfast on a "Table"? Or on a "Blueprint of a Table"? :p (I am sure it's a Table! because no one would have breakfast on a piece of paper).

    Next: you don't just create tables and throw them away! You store them - in what we call variables.

    If you do: my_table = Table.new - you are saving the Table in a variable that has been born for this current "method" or "function", once the execution is over, the variable is lost and gone. Usually.

    If you do: @my_table = Table.new - you are saving the Table in an OBJECT variable that will remain there for other "methods" to use later on. This table will persist in time!

    You can also look at @variables as "properties".

    Table probably has a variable @legs that equals to 4! (Well, because these Tables have 4 legs).

    If you set that a table has legs = 4, you cannot later ask how many legs the table has. Because when the "method" execution finished, the variable is lost. legs won't exist anymore.

    If you do @legs = 4, we can always later on ask "how many @legs does this table have?" and the answer will still be 4! :D

    Lastly, $variables are not object variables, but normally called "global" variables. Now, you cannot use these to determine how many legs a Table has. Because the variable is not stored inside an Object, but shared across all of them (if you changed the value somewhere, it will change for everywhere!)

    Hope that made sense :)

    Cheers!
  6. Thanks to all of your

    whitesphere said:
    Use a local variable when you do not want the value to be stored inside the object.  For example, if you perform a loop, the loop variable can be local, because you don't care about the value once the loop is finished.  These have no special markers in front, like loop=startValue

    Use an instance variable when you want each object of that type to remember the value.  For example, if you have a Point with an X and Y coordinate, each Point should store X and Y as instance variables, so all Point methods can refer to the proper X and Y values for that object.  These have a single @ mark in front, like @x=3

    Use a class variable when you want ALL objects of a type to share a value.  For example, if you have a counter that indicates how many Points you have, you could store that counter as a class variable in the Point class.  These are defined with 2 @ marks in front, like @@point_counter=0

    Use a global variable when you want OTHER objects to be able to read and modify a value.  For example, if you have a set of Points on a Canvas, and want only one Canvas to exist, you could use a global variable.  These are defined with 1 $ mark in front, like $single_canvas=Canvas.new()

    The .new method creates an instance of a particular object, by calling that object's initialize method with the parameters after the new, like:

    $single_canvas=Canvas.new(640, 480)

    This would call this:

    class Canvas

    # This method is called

    def initialize(width, height)

    end

    end

    I hope this helps.

    Engr. Adiktuzmiko said:
    It sets the method that would be run when a certain "command" is "pressed". It's used for Window_Selectable and it's child classes.

    Another Fen said:
    Local variables are used to temporarily store data during an operation. They are unique for every method call, so even if you use the same local variable names within multiple method definitions or process the same method several times at once, the calls will not interfere with each other.

    Instance variables are unique for every created instance of the class. They can be best compared to an events self switches: modifying a self switch within an event will only affect the event itself, but none of the other events that use the same self switch.

    In ruby, every object you create has its own set of instance variables. If you access an instance variable within a method, the "owner" of the variable will be the object you call the method upon:

    Spoiler
    class Lamp   # The initialize method is called when a new object is  # created by calling  Lamp.new  # By default, every lamp is turned off.  def initialize    @powered = false  end   # Turn the selected lamp on:  def turn_on    @powered = true  end  # Print info about the power status of the selected lamp:  def info    print "Lamp #{ self.object_id } is turned #{ @powered ? 'ON' : 'OFF' }!"  endend # --- TEST: ---lamp1 = Lamp.new  # Create an instance of Lamplamp2 = Lamp.new  # Create another instance of Lamp lamp2.turn_on  # Turn the second lamp on.lamp1.info# => "Lamp 20200090 is turned OFF!"lamp2.info# => "Lamp 20200080 is turned ON!"
    Spoiler
    Class variables are as such similar to global variables, but with some restrictions:
    - a class variable can only be accessed directly within the class itself (or one of their subclasses).
    - trying to read an undefined class variable will result in an error (may reveal typing errors)
    - independent classes can use the same class variable names without interfering with each other.
     

    PabloNeirotti said:
    Just for fun, let me explain it my way :D

    .new creates a new "instance" of the Object.

    Ruby is the purest (modern) Object-oriented programming language. You work with objects all the time. A party Actor is an object, who has it's own HP and Mana.

    Let's say there is a class called Table. This defines how a Table should behave or look like.

    If you do: Table.new - then voilá! You just created a Table. In programming you can create as many as you want immediately :D

    Now, Table alone is the blueprint to create a "new" table, but it's not a table on itself. Let me ask you this: Do you have breakfast on a "Table"? Or on a "Blueprint of a Table"? :p (I am sure it's a Table! because no one would have breakfast on a piece of paper).

    Next: you don't just create tables and throw them away! You store them - in what we call variables.

    If you do: my_table = Table.new - you are saving the Table in a variable that has been born for this current "method" or "function", once the execution is over, the variable is lost and gone. Usually.

    If you do: @my_table = Table.new - you are saving the Table in an OBJECT variable that will remain there for other "methods" to use later on. This table will persist in time!

    You can also look at @variables as "properties".

    Table probably has a variable @legs that equals to 4! (Well, because these Tables have 4 legs).

    If you set that a table has legs = 4, you cannot later ask how many legs the table has. Because when the "method" execution finished, the variable is lost. legs won't exist anymore.

    If you do @legs = 4, we can always later on ask "how many @legs does this table have?" and the answer will still be 4! :D

    Lastly, $variables are not object variables, but normally called "global" variables. Now, you cannot use these to determine how many legs a Table has. Because the variable is not stored inside an Object, but shared across all of them (if you changed the value somewhere, it will change for everywhere!)

    Hope that made sense :)

    Cheers!
    thanks to all of your answers! I think, i got it now^.^