Prev: SSL communication with Mechanize
Next: ri odd behavior
From: Raghu Maddali on 25 Jul 2010 06:23 Hi all, Is there anything in ruby like we use "this" pointer in ruby.Like,how do we write $this->config = parse_ini_file('web/display.ini', true) in ruby?? Thanks -- Posted via http://www.ruby-forum.com/.
From: Josh Cheek on 25 Jul 2010 08:18 [Note: parts of this message were removed to make it a legal post.] On Sun, Jul 25, 2010 at 5:23 AM, Raghu Maddali <raghu1216(a)yahoo.co.in>wrote: > Hi all, > > Is there anything in ruby like we use "this" pointer in ruby.Like,how do > we write > > $this->config = parse_ini_file('web/display.ini', true) > > in ruby?? > > Thanks > -- > Posted via http://www.ruby-forum.com/. > > presumably you want self.config = parse_ini_file(...) your actual question is a little unclear (I assume you meant to refer to a different language) but you should note that there are no pointers in Ruby, and in the preceeding example, "config=" is a method defined on self, you are not directly setting an instance variable like you would be in Java, but rather invoking a method which probably sets an instance variable, though not necessarily.
From: Raghu Maddali on 25 Jul 2010 08:41 I apologize for miscommunication.I know that Ruby will not have any pointers and I guess its a variable declared with private access modifier.Its like private $config; $this->config = parse_ini_file('conf/Voyager.ini', true); So,how would you code this in Ruby?Is that the same private $config $self.config=parse_ini_file(....)??? Raghu > presumably you want self.config = parse_ini_file(...) your actual > question > is a little unclear (I assume you meant to refer to a different > language) > but you should note that there are no pointers in Ruby, and in the > preceeding example, "config=" is a method defined on self, you are not > directly setting an instance variable like you would be in Java, but > rather > invoking a method which probably sets an instance variable, though not > necessarily. -- Posted via http://www.ruby-forum.com/.
From: Fan Zhang on 25 Jul 2010 12:35 If you want to access private member variables of a class instance, using @ A quick search reveals this site: http://www.railsrocket.com/private-member-variables-in-ruby On Sun, Jul 25, 2010 at 8:41 PM, Raghu Maddali <raghu1216(a)yahoo.co.in> wrote: > I apologize for miscommunication.I know that Ruby will not have any > pointers and I guess its a variable declared with private access > modifier.Its like > > private $config; > $this->config = parse_ini_file('conf/Voyager.ini', true); > > So,how would you code this in Ruby?Is that the same > > private $config > $self.config=parse_ini_file(....)??? > > Raghu > >> presumably you want self.config = parse_ini_file(...) your actual >> question >> is a little unclear (I assume you meant to refer to a different >> language) >> but you should note that there are no pointers in Ruby, and in the >> preceeding example, "config=" is a method defined on self, you are not >> directly setting an instance variable like you would be in Java, but >> rather >> invoking a method which probably sets an instance variable, though not >> necessarily. > > -- > Posted via http://www.ruby-forum.com/. > >
From: Josh Cheek on 25 Jul 2010 17:11
[Note: parts of this message were removed to make it a legal post.] On Sun, Jul 25, 2010 at 7:41 AM, Raghu Maddali <raghu1216(a)yahoo.co.in>wrote: > I apologize for miscommunication.I know that Ruby will not have any > pointers and I guess its a variable declared with private access > modifier.Its like > > private $config; > $this->config = parse_ini_file('conf/Voyager.ini', true); > > So,how would you code this in Ruby?Is that the same > > private $config > $self.config=parse_ini_file(....)??? > > Raghu > > > presumably you want self.config = parse_ini_file(...) your actual > > question > > is a little unclear (I assume you meant to refer to a different > > language) > > but you should note that there are no pointers in Ruby, and in the > > preceeding example, "config=" is a method defined on self, you are not > > directly setting an instance variable like you would be in Java, but > > rather > > invoking a method which probably sets an instance variable, though not > > necessarily. > > -- > Posted via http://www.ruby-forum.com/. > > In Ruiby, all instance variables are private (hence the need to use the #config= method) From within an object, you can access its private instance variables directly by suing the @ sigil @config = parse_ini_file(...) from outside the object, you must use a method. class Person def name=(newname) @name = newname end def name @name end end person = Person.new # invoke the #name= method, which will set @name person.name = 'Josh' # invoke the #name method, which will get @name person.name # => "Josh" |