Class arguments (newbe question)

  • Thread starter Panagiotis Atmatzidis
  • Start date
P

Panagiotis Atmatzidis

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello to the list!

I have the following code which prints the ip's from fail2ban.log and =
puts then in an array:

- --------------------------
class ReadIPs
attr_accessor :ip
def initialize(ip)
@ip =3D ip
end
=20
def ip(filename)=20
ips =3D []
File.read(filename).lines.to_a.each do |place|
sf =3D 0
while sfn =3D =
place.index(/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|=
[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4]=
[0-9]|[01]?[0-9][0-9]?)/,sf)
sf =3D sfn + 3
ips << $&
end
end
return ips
end
end

# print methods

a =3D ReadIPs.new("/Users/atma/Projects/ZoneReport/log/fail2ban.log")
puts a.ip("/Users/atma/Projects/ZoneReport/log/fail2ban.log")

- ---------------------------------

What I don't understand is why do I need to give the argument to the new =
instance of ReadIPs.new class. In my view the code should look like:

a =3D ReadIPs.new
puts a.ip("/Users/atma/Projects/ZoneReport/log/fail2ban.log")
- ----------------------------------

But this returns an error and does not run. Can someone please drop a =
few light here please :)

best regards & thanks in advance

Panagiotis (atmosx) Atmatzidis

email: (e-mail address removed)
URL: http://www.convalesco.org
GnuPG ID: 0xFC4E8BB4=20
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xFC4E8BB4
- --
The wise man said: "Never argue with an idiot. They bring you down to =
their level and beat you with experience."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.12 (Darwin)

iEYEARECAAYFAksaJSAACgkQrghUb/xOi7QoBwCffljNmCgWiDEtrmYfvyt7wjFy
8R0An0//JC5C3lH/JqJwX13PAQwTS07l
=3DmI8+
-----END PGP SIGNATURE-----
 
J

Jesús Gabriel y Galán

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello to the list!

I have the following code which prints the ip's from fail2ban.log and put= s then in an array:

- --------------------------
class ReadIPs
=A0attr_accessor :ip
=A0def initialize(ip)
=A0 =A0@ip =3D ip
=A0end

When you define an initialize method that receives an argument, you
must pass an argument when you create an instance with new.
=A0def ip(filename)
=A0 =A0ips =3D []
=A0 =A0File.read(filename).lines.to_a.each do |place|
=A0 =A0 =A0sf =3D 0
=A0 =A0 =A0while sfn =3D place.index(/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9= ]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9=
][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/,sf)
=A0 =A0 =A0 =A0sf =3D sfn + 3
=A0 =A0 =A0 =A0ips << $&
=A0 =A0 =A0end
=A0 =A0end
=A0 =A0 =A0return ips
=A0end
end

# print methods

a =3D ReadIPs.new("/Users/atma/Projects/ZoneReport/log/fail2ban.log")
puts a.ip("/Users/atma/Projects/ZoneReport/log/fail2ban.log")

- ---------------------------------

What I don't understand is why do I need to give the argument to the new =
instance of ReadIPs.new class. In my view the code should look like:
a =3D ReadIPs.new
puts a.ip("/Users/atma/Projects/ZoneReport/log/fail2ban.log")

Then you can remove the initialize method from the ReadIPs class. In
fact, if that's the only thing you are going to do, you don't even
need a to be an instance. You can have a class method instead, since
you are not maintaining state:

class ReadIPs
def self.ip(filename)
...
end
end

and call it like

ReadIPs.ip("/Users/atma/Projects/ZoneReport/log/fail2ban.log")

Another possibility is to maintain the calculated array of matched IPs
in an instance, so what I would do in that case is:

class ReadIPs
attr_reader :ips

def initialize file_name
@ips =3D perform the algorithm to match the ips.
end
end

and then you can do:

a =3D ReadIPs.new("/Users/atma/Projects/ZoneReport/log/fail2ban.log")

which will calculate the ips and store them in the instance variable.
Then you can call:

a.ips

which will return the stored array.

Hope this helps,

Jesus.
 
P

Panagiotis Atmatzidis

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
=20
Hello to the list!
=20
I have the following code which prints the ip's from fail2ban.log and = puts then in an array:
=20
- --------------------------
class ReadIPs
attr_accessor :ip
def initialize(ip)
@ip =3D ip
end
=20
When you define an initialize method that receives an argument, you
must pass an argument when you create an instance with new.
=20
=20
def ip(filename)
ips =3D []
File.read(filename).lines.to_a.each do |place|
sf =3D 0
while sfn =3D =
place.index(/(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|=
[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4]=
[0-9]|[01]?[0-9][0-9]?)/,sf)
sf =3D sfn + 3
ips << $&
end
end
return ips
end
end
=20
# print methods
=20
a =3D ReadIPs.new("/Users/atma/Projects/ZoneReport/log/fail2ban.log")
puts a.ip("/Users/atma/Projects/ZoneReport/log/fail2ban.log")
=20
- ---------------------------------
=20
What I don't understand is why do I need to give the argument to the = new instance of ReadIPs.new class. In my view the code should look like:
=20
a =3D ReadIPs.new
puts a.ip("/Users/atma/Projects/ZoneReport/log/fail2ban.log")
=20
Then you can remove the initialize method from the ReadIPs class. In
fact, if that's the only thing you are going to do, you don't even
need a to be an instance. You can have a class method instead, since
you are not maintaining state:
=20
class ReadIPs
def self.ip(filename)
...
end
end
=20
and call it like
=20
ReadIPs.ip("/Users/atma/Projects/ZoneReport/log/fail2ban.log")
=20
Another possibility is to maintain the calculated array of matched IPs
in an instance, so what I would do in that case is:
=20
class ReadIPs
attr_reader :ips
=20
def initialize file_name
@ips =3D perform the algorithm to match the ips.
end
end
=20
and then you can do:
=20
a =3D ReadIPs.new("/Users/atma/Projects/ZoneReport/log/fail2ban.log")
=20
which will calculate the ips and store them in the instance variable.
Then you can call:
=20
a.ips
=20
which will return the stored array.
=20
Hope this helps,

It helped a lot! Thank you for the prompt & detailed answer


Panagiotis (atmosx) Atmatzidis

email: (e-mail address removed)
URL: http://www.convalesco.org
GnuPG ID: 0xFC4E8BB4=20
gpg --keyserver x-hkp://pgp.mit.edu --recv-keys 0xFC4E8BB4
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top