From: Raghu Maddali on
> 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


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

From: Jesús Gabriel y Galán on
On Mon, Jul 26, 2010 at 7:06 AM, Raghu Maddali <raghu1216(a)yahoo.co.in> 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.

From: Jarmo Pertman on
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 Pertman
IT does really matter - http://www.itreallymatters.net


On Jul 26, 9:37 am, Jesús Gabriel y Galán <jgabrielyga...(a)gmail.com>
wrote:
> On Mon, Jul 26, 2010 at 7:06 AM, Raghu Maddali <raghu1...(a)yahoo.co.in> 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.

From: Raghu Maddali on
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
--
Posted via http://www.ruby-forum.com/.

From: Jesús Gabriel y Galán on
On Tue, Jul 27, 2010 at 7:37 PM, Raghu Maddali <raghu1216(a)yahoo.co.in> 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.

First  |  Prev  | 
Pages: 1 2
Prev: SSL communication with Mechanize
Next: ri odd behavior