syntax

R

Raghu Maddali

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
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.
 
R

Raghu Maddali

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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
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"
 
R

Raghu Maddali

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
 
J

Jesús Gabriel y Galán

Hey Josh,

My declaration will be something like

Class Web

class Web
=A0 =A0 Private $conf
=A0 =A0 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 $.
=A0 =A0 def __construct()


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

def initialize
=A0 =A0 =A0@config =3D parse_ini_file('web/display.ini', true)
=A0 =A0 =A0@database =3D @config['Catalog']['database']
=A0 =A0 end
end


This would be fine.
Will this be a correct Ruby syntax

Summary:

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

Jesus.
 
J

Jarmo Pertman

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


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.
 
R

Raghu Maddali

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
 
J

Jesús Gabriel y Galán

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

class Web
=A0 =A0 =A0@@config
=A0 =A0 =A0@@database

You don't need to declare the variables.
=A0 def initialize
=A0 =A0 @@config =3D parse_ini_file('web/display.ini', true)
=A0 =A0 @@database =3D @@config['Catalog']['database']
=A0 end
=A0 =A0def methodA()
=A0 =A0 @@database=3D......
=A0 =A0end
=A0end

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top