PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > How do I make variables defined in a method accessible on anothermethod?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How do I make variables defined in a method accessible on anothermethod?

Réponse
 
LinkBack Outils de la discussion
Vieux 04/11/2007, 03h51   #1
victor.reyes@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How do I make variables defined in a method accessible on anothermethod?

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Team,

I would like to initialize some variables/arrays in a method, and access
them within another method.
For instance,


class My_Class

def init_method(size)
arr = Array.new(size)
index = 1
end # End method

def print_method
p arr
p index
end # End method

end # End class

my_obj = My_Class.new()
my_obj.init_method 10
my_obj.print_method


When I attempt to execute this code, I receive the error:

ruby My_Class
My_Class:9:in `print_method': undefined local variable or method `arr' for
#<My_Class:0x28ee87c> (NameError)
from My_Class:17


I know the reason why it is happening, but I don't know how to access a
variable from another method, if there is a way.

Thank you

Victor

  Réponse avec citation
Vieux 04/11/2007, 04h21   #2
mbj@seonic.net
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do I make variables defined in a method accessible onanother method?

Am Sun, 4 Nov 2007 12:51:19 +0900
schrieb "Victor Reyes" <victor.reyes@gmail.com>:

> class My_Class
>
> def init_method(size)
> arr = Array.new(size)
> index = 1
> end # End method
>
> def print_method
> p arr
> p index
> end # End method
>
> end # End class
>
> my_obj = My_Class.new()
> my_obj.init_method 10
> my_obj.print_method


Use instance variables!

class Example
def init(size) # why u arent using initialize?
@arr = Array.new
@index = -1
end

def print_method
p @arr
p @index
end
end # class Example

  Réponse avec citation
Vieux 04/11/2007, 04h37   #3
list.push@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do I make variables defined in a method accessible onanother method?

On 11/4/07, Victor Reyes <victor.reyes@gmail.com> wrote:
> Team,
>
> I know the reason why it is happening, but I don't know how to access a
> variable from another method, if there is a way.


class My_Class
def initialize(size)
@arr = Array.new(size)
@ind = 1
end

def print_method
p @arr
p @ind
end
end

my_obj = My_Class.new(10)
my_obj.print_method


Harry


--
A Look into Japanese Ruby List in English
http://www.kakueki.com/

  Réponse avec citation
Vieux 04/11/2007, 07h50   #4
bbxx789_05ss@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do I make variables defined in a method accessible onanother method?

Markus Schirp wrote:
> class Example
> def init(size) # why u arent using initialize?
> @arr = Array.new
> @index = -1
> end


Just to flesh out that comment. Examine this class:

class Dog
def init_method(age, weight)
@age = age
@weight = weight
end

def show
puts @age, @weight
end
end


d = Dog.new
d.init_method(2, 20)
d.show

--output:--
2
20


However, when you create an object, ruby *automatically* calls a method
called initialize() in the object's class:

class Dog
def initialize(age, weight)
@age = age
@weight = weight
end

def show
puts @age, @weight
end
end


d = Dog.new(2, 20)
d.show

--output:--
2
20


--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 04/11/2007, 13h34   #5
victor.reyes@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How do I make variables defined in a method accessible onanother method?

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

First, thank you to Markus, Harry and 7stud for your .
I truly appreciate it.

Markus, answering your question "why u arent using initialize?" SIMPLE: I
didn't know how to use it.

Again, Thank you all!

Victor

On 11/3/07, Victor Reyes <victor.reyes@gmail.com> wrote:
>
> Team,
>
> I would like to initialize some variables/arrays in a method, and access
> them within another method.
> For instance,
>
>
> class My_Class
>
> def init_method(size)
> arr = Array.new(size)
> index = 1
> end # End method
>
> def print_method
> p arr
> p index
> end # End method
>
> end # End class
>
> my_obj = My_Class.new()
> my_obj.init_method 10
> my_obj.print_method
>
>
> When I attempt to execute this code, I receive the error:
>
> ruby My_Class
> My_Class:9:in `print_method': undefined local variable or method `arr' for
> #<My_Class:0x28ee87c> (NameError)
> from My_Class:17
>
>
> I know the reason why it is happening, but I don't know how to access a
> variable from another method, if there is a way.
>
> Thank you
>
> Victor
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12851 seconds with 13 queries