Syntax

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

On Sun, Jul 25, 2010 at 5:23 AM, Raghu M.
[email protected]wrote:


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.

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.

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 7:41 AM, Raghu M.
[email protected]wrote:

$self.config=parse_ini_file(…)???

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”

On Mon, Jul 26, 2010 at 7:06 AM, Raghu M. [email protected]
wrote:

from outside the object, you must use a method.

Hey Josh,

My declaration will be something like

Class Web

class Web

Private $conf
Private $database

You don’t need to declare the instance variables. And in any case
Private is not a Ruby keyword. And $var is a global variable. Just
remove all the $.

def __construct()

If you want this to be your class’ constructor, the method is called
initialize, so

def initialize

 @config = parse_ini_file('web/display.ini', true)
 @database = @config['Catalog']['database']
end

end

This would be fine.

Will this be a correct Ruby syntax

Summary:

class Web
def initialize
@config = parse_ini_file(‘web/display.ini’, true)
@database = @config[‘Catalog’][‘database’]
end
end

Jesus.

And also, is you want to have access to @config and @database from the
outside, then as already mentioned then all instance variables (the
ones with @) are private. That means you have to use methods to set
and get the values of those variables. So you could create a methods
like this:

class Web
def config
@config
end

def config=(new_config)
@config = new_config
end

end

But, since most of the time the get and set methods are defined just
like the ones above then you can create those methods semi-
automatically by an accessor methods. So, to define an get and set
methods for @config and @database, you can do it like this:

class Web
attr_accessor :config, :database
end

Or if you want just a get methods use “attr_reader” instead. And for a
write-only access “attr_writer”.

Enjoy Ruby!


Jarmo P.
IT does really matter - http://www.itreallymatters.net

On Jul 26, 9:37 am, Jesús Gabriel y Galán [email protected]

I’ve conf and database as class variables which I access in some other
methods too…So can I write it as

class Web
@@config
@@database
def initialize
@@config = parse_ini_file(‘web/display.ini’, true)
@@database = @@config[‘Catalog’][‘database’]
end
def methodA()
@@database=…
end
end

Raghu

from outside the object, you must use a method.

Hey Josh,

My declaration will be something like

Class Web
Private $conf
Private $database

 def __construct()

  @config = parse_ini_file('web/display.ini', true)
  @database = @config['Catalog']['database']
 end

end

Will this be a correct Ruby syntax

Thanks

On Tue, Jul 27, 2010 at 7:37 PM, Raghu M. [email protected]
wrote:

I’ve conf and database as class variables which I access in some other
methods too…So can I write it as

class Web
@@config
@@database

You don’t need to declare the variables.

def initialize
@@config = parse_ini_file(‘web/display.ini’, true)
@@database = @@config[‘Catalog’][‘database’]
end
def methodA()
@@database=…
end
end

If they are class variables, it usually doesn’t make a lot of sense to
initialize them every time you create an instance of this class.
Maybe the initialization of those variables should be put outside the
initialize method, or if the values they might have could be different
on each instance of the class, then leave them as instance variables.

Jesus.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs