Function CWSLogin(strApplicationPath,strUser,strMember,strPassword)
Class TryCatchFinally
Private Sub Class_Initialize 'Try
End Sub
End Class
End Function
No. Functions are in classes where they become known as methods.
Related
I am developing a VBScript like follows
Dim a
Set a = New Class1
a("text").doSomething 'Has to execute doSomething in Class1
a("text").anotherSomething 'Has to execute doSoemthing in Class2
class Class1
Dim b
Dim c
public default Function init(str)
Set b = New Class2
Set c = New Class3
'Some more operations to perform
If **What is the condition can be?** Then
Set init = c
Else
Set init = b
End If
End Function
End class
class Class2
public Function doSomething()
'Stuff to do something
End Function
End class
class Class3
public Function anotherSomething()
'Stuff to do something
End Function
End class
Here, Object "a" has parameters and this parameter can be same.
So, i can't keep is parameter "=" or "<>"
And, I can't place those functions in Class1.
So, What can be that condition which can decide.
It looks like you need to leverage delegators. Basically you need internal objects that are of the type you want to access methods that would be in your parent classes in OOP.
Here is an example:
Class ScreenPoint
'' Properties
'Ancestor Point2D
Private P2D
'Point color
Private Color
'----------------------
'' Methods
'Constructor - called automatically
Private Sub Class_Initialize()
Set P2D = new Point2D
End Sub
'----------------------
'Destructor - called automatically
Private Sub Class_Terminate()
Set P2D = Nothing
End Sub
'----------------------
'A pair of methods to access private property X
Property Get X
X = P2D.X
End Property
Property Let X(ByVal in_X)
P2D.X = in_X
End Property
'----------------------
'A pair of methods to access private property Y
Property Get Y
Y = P2D.Y
End Property
Property Let Y(ByVal in_Y)
P2D.Y = in_Y
End Property
'----------------------
End Class
If you are lost, try reading the source article here: http://automation-beyond.com/2008/11/16/oop-vbscript-2/
I was hoping to extract a lot of logic into a separate class in the following code, but I am having trouble with that since it ends up calling a protected method.
This is my current code:
class ExcelSheet
...
protected
def save_stuff
# do work
end
end
class CustomSheet < ExcelSheet
def custom_stuff
# lots of logic
save_stuff
# more logic
end
end
This was my attempted code:
class LogicManager
def logic_valid?
# lots of logic
save_stuff
end
end
class CustomSheet < ExcelSheet
def custom_stuff
manager = LogicManager.new(some_data)
if manager.logic_valid?
# more logic
end
end
end
Unfortunately in my LogicManager I can't call save_stuff because it's protected. I didn't write the original method, and I'm sure it's marked as protected for a reason, so I don't think I should change that.
What options do I have to still refactor nicely?
It seems that you have a bit of a misunderstanding of protected methods.
Protected methods can be invoked only by the class that defines it, or by it's subclasses. In your case, it means that if you would like to invoke the method save_stuff in the class LogicManager, that means that LogicManager must inherit from ExcelSheet.
Given the naming of the classes, I am not sure if you want to do that as it doesn't seem logical to me (no pun intended).
I see there are two ways to specify access control in Ruby. One with access modifier specified with the method and the other as a list to the ACL function.
E.g. with the access modifier specified with the method
class MyClass
public
def method1
#...
end
private
def method2
#...
end
end
and another way where you specify the access as a list to the ACL function
class MyClass
def method1
#...
end
def method2
#...
end
public :method1
private :method2
end
Is there any reason to use one over the other?
Normally, you use the first method you described. You would first have all public methods grouped together (without public specified, since it is the default at the beginning), followed by all protected methods grouped together, finally followed by all private methods grouped together. An example:
class C
def first_public_method
#...
end
def second_public_method
#...
end
protected
def protected_method_1
#...
end
def protected_method_2
#...
end
private
def one_private_method
#...
end
def another_private_method
#...
end
end
The second method you described in your question is rarely used, mostly for doing some tricks. It allows you to do the following things for example that are impossible with the first method:
Dynamically build a list of methods that should have a certain visibility and then set that visibility by calling the appropriate method with the dynamic list, e.g. public(*an_array_containing_names_of_all_methods_that_should_be_public)
Change the visibility of a method after a class has been defined
An example for the second point above:
class C
#...
private
def a_private_method
#...
end
end
instance = C.new
instance.a_private_method # raises NoMethodError
C.send(:public, :a_private_method) # changes the visibility of the method dynamically to public
instance.a_private_method # executes the formerly private method without error
If specify the modifier with the method, it's right there with the method definition, not hidden away somewhere. Conventionally you might groupall the private methods at the bottom of the file with one private modifier as these modifiers like private, public, module_function (others?) work as "everything below this line until you meet a contradictory one".
Alternately, you could use the method form but then I'd place them as near to the top as possible so it's obvious. Hang on, would that lead into a "this method isn't defined, so what the hell am I private-ing???" exception? In that case I'd group them at the bottom. The point being to not scatter them in the middle.
I have two separate classes (A = a subclass of BevelButtom, B = a subclass of PushButton). Both A and B implement a number of identical methods in exactly the same way. Since both subclasses' superclasses are different and since RB doesn't support multiple inheritance, all I can do to tie these methods together is define a class interface, have both subclasses implement the interface, then copy/paste the method bodies in each subclass.
That offends my sensibilities. Is there a way in RB to extract this common logic elsewhere?
Thanks!
Use the Extends syntax from a module method to extend the class interface. You still need to use a class interface, but this way all the common code can be placed in the module instead of being duplicated across multiple classes.
Interface FooInterface
Sub Foo()
End Interface
Class Foo
Implements FooInterface
Sub Foo()
MsgBox("Foo!")
End Sub
End Class
Class Bar
Implements FooInterface
Sub Foo()
MsgBox("Bar!")
End Sub
End Class
Module FooExtensions
Sub Foobar(Extends FooImplementor As FooInterface)
MsgBox("Foobar!")
End Sub
End Module
The above FooBar method would be called like a class method of any class that implements the FooInterface class interface:
Dim myfoo As FooInterface = New Bar
myfoo.Foobar()
Note that extension methods don't count when the compiler is deciding whether a given class satisfies an interface.
This may not be workable, however, since the extension methods will only have access to the Interface rather than the actual class.
Alternatively, you could extend the RectControl class, though that would include all desktop controls, not just the PushButton and BevelButton.
A third option might be to use the BevelButton class exclusively.
Using an interface seems like the right approach, but rather than copying the method bodies to each subclass, I think it makes more sense to create a new class (say CommonButtonStuff) with the common code. Then you can call it in the implemented methods:
CommonButtonInterface
Sub Method1
Class CommonButtonHandler
Sub DoMethod1
MsgBox("Do it!")
End Sub
Class A Inherits From PushButton, Implements CommonButtonInterface
Private Property mCommonStuff As CommonButtonHandler
Sub Constructor
mCommonStuff = New CommonButtonHandler
End Sub
Sub Method1
mCommonStuff.DoMethod1
End Sub
Class B Inherits From BevelButton, Implements CommonButtonInterface
Private Property mCommonStuff As CommonButtonHandler
Sub Constructor
mCommonStuff = New CommonButtonHandler
End Sub
Sub Method1
mCommonStuff.DoMethod1
End Sub
I want a method to be executed every 5 minutes, I implemented whenever for ruby (cron). But it does not work. I think my method is not accessible.
The method I want to execute is located in a class. I think I have to make that method static so I can access it with MyClass.MyMethod. But I can not find the right syntax or maybe I am looking in the wrong place.
Schedule.rb
every 5.minutes do
runner "Ping.checkPings"
end
Ping.rb
def checkPings
gate = Net::Ping::External.new("10.10.1.1")
#monitor_ping = Ping.new()
if gate.ping?
MonitorPing.WAN = true
else
MonitorPing.WAN = false
end
#monitor_ping.save
end
To declare a static method, write ...
def self.checkPings
# A static method
end
... or ...
class Myclass extend self
def checkPings
# Its static method
end
end
You can use static methods in Ruby like this:
class MyModel
def self.do_something
puts "this is a static method"
end
end
MyModel.do_something # => "this is a static method"
MyModel::do_something # => "this is a static method"
Also notice that you're using a wrong naming convention for your method. It should be check_pings instead, but this does not affect if your code works or not, it's just the ruby-style.
Change your code from
class MyModel
def checkPings
end
end
to
class MyModel
def self.checkPings
end
end
Note there is self added to the method name.
def checkPings is an instance method for the class MyModel whereas def self.checkPings is a class method.
Instead of extending self for the whole class, you can create a block that extends from self and define your static methods inside.
you would do something like this :
class << self
#define static methods here
end
So in your example, you would do something like this :
class Ping
class << self
def checkPings
#do you ping code here
# checkPings is a static method
end
end
end
and you can call it as follows : Ping.checkPings
There are some ways to declare a static method in RoR.
#1
class YourClassName
class << self
def your_static_method (params)
# Your code here
end
end
end
#2
class YourClassName
def self.your_status_method
// Your code here
end
end
You cannot have static methods in Ruby. In Ruby, all methods are dynamic. There is only one kind of method in Ruby: dynamic instance methods.
Really, the term static method is a misnomer anyway. A static method is a method which is not associated with any object and which is not dispatched dynamically (hence "static"), but those two are pretty much the definition of what it means to be a "method". We already have a perfectly good name for this construct: a procedure.