MDBitz - Matthew Denton
Ruby
Most developers are aware of the Switch statement that compares a variable to specific cases and performs logic based on if it matches the case criteria. In Ruby they decided to implement this as simply a Case statement with when clauses specifying logic to be performed if the when clause is true.
case year
when 2000
puts "The world was supposed to end this year!"
when 2012
puts "Now the world is supposed to end this year!"
else
puts "I wonder what year the world will supposed to end after 2012?"
end
As you can see from the above example it functions the same as in other languages with a slightly different syntax where instead of default you use end and instead of cases you use when
Case statements with multi value When Clauses
In Java, PHP and other programming languages the case statements can fall through so that you can define some logic to occour for multiple values. Ruby is no different in that it too allows you to have multiple values in each when statement the difference is you simply sperate the values by a ,(comma) when defining your when statements.
case year
when 2000, 2012
puts "The world is going to end!"
else
puts "Is the world going to end?"
end
Case statement with no value defined
If you simply prefer to write case statements instead of if statements then you can define a case statement that has no value and instead can specify the comparison logic in each when clause.
aString = "testing"
case
when aString="testing"
puts "What are you testing."
else
puts "This is for real everyone!"
end
The Case statement in Ruby breaks no new ground however it does provide you with an alternative method of writing your basic if statements. Good to know but as for which you should utilize it is really up to your own preference.
Case, My Self-Inflicted crash course introduction into Ruby, Ruby, switch
Arrays in Visual Basic are handled differently then their counter parts in Java, PHP, and Ruby. The first difference is that you specify array index by use of parenthesis not square brackets.
Java Array Initialization
String[] myArray = new String[20];
Visual Basic Array Initialization
Dim myArray(19) As String
Looking over the two arrays you may think that the Array in VB only can hold 19 elements this is incorrect. Visual Basic uses a 0 based index meaning that in actuality it holds elements at indexes 0 through 19 or 20 total items.
Fixed Length Array
When you specify the array’s dimension when declaring the array you have created it as a Fixed length Array this means that you can not change its size and decide you want to add a 21st item to an array of 20 items. To access or set an item in an array you simply need to call the variable with it’s index wrapped in parenthesis.
Dim myArray(2) As String
myArray(0) = "Matthew"
myArray(1) = "Apollo"
myArray(2) = "Joseph"
Dim aName As String
aName = myArray(1)
Dynamic Length Array
Sometimes you will not know the full size of the array you will need to create. In these cases you can create a Dynamic Length Array, these arrays allow you to change their size by re defining their length while preserving their contents. To define your Dynamic Array you simply don’t specify the length of the array in its initial creation.
Now before you can use the array to store elements you will need to redefine it with a length to do this you will need to use the ReDim command.
ReDim myArray(2) As String
If we need to increase the size again we would simply use ReDim again, however this will cause any existing data to be lost. If you want to keep all current elements in the dynamic you will need to use the Preserve keyword.
ReDim Preserve myArray(5) As String
Array Utility/Helper Functions
Split
If you have a delimited String that you want to break up based on a character the Split function is what you will want to use. It takes a String and a delimiter and returns an Array containing sub strings between the delimiter.
Dim userArray() As String
Dim userNames As String
nameArray= "Matthew,Doug,Sarah,Jill"
userArray = Split( nameArray, "," )
' The generated array would contain 4 elements
userArray[0] 'Matthew
userArray[1] 'Doug
userArray[2] 'Sarah
userArray[3] 'Jill
Join
Conversely you may find that you want to take an array and concatenate the elements with some value between the elements. In this case you would use the Join method. If you don’t want any delimiter between the values you simply don’t input a 2nd parameter and empty string will be used by default.
Dim userArray(3) As String
Dim userNames As String
userArray[0] = Matthew
userArray[1] = Doug
userArray[2] = Sarah
userArray[3] = Jill
userNames = Join(userArray, "," )
usernames 'Matthew,Doug,Sarah,Jill
As shown Visual Basics has all the common functionality of arrays in other programming languages the key to remember is that you use parenthesis not square brackets when accessing element indexes.
Array, Dynamic Array, Fixed Length Array, preserve, ReDim
Visual Basic allows multiple methods for concatenating Strings. The simplest and most easily recognized method is using the + operator which will perform the concatenation of Strings. However if you want to concatenate two numbers together you will first have to convert them to Strings to use the + operator. To enable you to specify explicitly that you want to perform a String concatenation Visual Basic has the & operator.
The & operator
The & operator specifies that you want to perform String Concatenation of the objects. Using this operator the objects must be convertible to a String, and if the objects are null then an empty String is returned.
Dim testStr As String
testStr = "My " & "Number: " & 3214
In addition to the & operator you can use the &= operator if you want to append data to the end of a String.
Dim testStr As String
testStr = "Hello"
testStr &= " World"
String Builder
In the case that your application is performing massive String Concatenations then you might get better performance in utilizing the System.Text.StringBuilder Class. This class allows you append, prepend, replace, and in general manipulate the underlying String Data.
Dim sb As New StringBuilder()
sb.Append("Hello")
sb.Append(" World!")
sb.ToString()
String Concatenation is a basic task performed in most applications. Visual Basic enables you to utilize multiple methods for completing the task including the & operator that will force String Concatenation of Integers and Doubles.
& operator, Concatenation, StringBuilder, VB, Visual Basic
MDBitz - Matthew Denton
Ruby
One of the most integral parts to programming is the basic IF statement or the conditional Statement. Ruby like most if not all languages uses the if keyword to define an IF statement. However it defers from other languages as you can use the if keyword as a statement or a modifier. In addition Ruby has the unless keyword to perform the else if logic without testing for a negative result.
The basic IF ELSE statement
The IF statement in Ruby is the same as Java or C++ with the following differences like methods you don’t need to utilize parenthesis and ELSE IF has been shortened into the elsif keyword. Lets start by looking at a basic example:
if var < 10
puts "The variable is less than ten."
elsif var >= 10 and var < 25
puts "The variable is less than 25 but greater or equal to ten."
else
puts "The variable is greater then or equal to 25."
end
The IF modifier
In addition to defining IF statements ruby lets you use conditionals as a modifier. For example you can write your command and append the if modifier so that the command only executes if the test conditional is true.
puts "The variable is less than 0" IF var < 0
In the example we have written our code for outputting our string but modified it by saying only output it IF our condition is met.
The UNLESS keyword
Ruby in addition to implementing the basic IF ELSE conditional has added in the unless keyword to their language. This keyword is used to say execute the following code if and only if this condition is not met.
if var < 0
puts "The variable is greater than 0"
else
puts "The variable is less than 0"
end
The unless keyword is really only a convenience method for those cases your are testing for false, so instead of negating the conditional statement you can simply use unless. Like the if keyword it can also be used as a modifier.
puts "The variable is less than 0" unless var >= 0
Not sure why it was determined that the unless keyword exists in Ruby as everything it can be used for can be completed with an if statement. However from what I have seen Ruby tries to let developers do whatever they want without imposing any restrictions so i guess writing unless is easier then a negated conditional for some.
else, elsif, if, modifier, My Self-Inflicted crash course introduction into Ruby, Ruby, statement, unless
MDBitz - Matthew Denton
Ruby
When learning Ruby with a background in Java/C# .NET and similar high level object oriented programming languages method visibility can be a major stumbling block. To start things off all Ruby methods are by default public or accessible by any object. To limit access you have the option of modifying a methods visibility to either protected or private. Before we dive into what they mean lets look at the two ways to modify method visibility.
Modifying Method Visibility
To begin lets work with sample class that has 3 methods
class SampleClass
def methodOne
end
def methodTwo
end
def methodThree
end
end
By default all the methods of this class are public and accessible by anyone. To modify the method scope we need to use the private or protected modifier methods / keywords.
Named Methods
To define a single method or methods as private/protected you simply
pass a comma separated list of named methods using the syntax :methodName.
class SampleClass
def methodOne
end
def methodTwo
end
def methodThree
end
private :methodTwo, :methodThree
end
Methods Following Keyword
If the private/protected method modifiers are used without named methods then any method defined after the keyword will have that scope. For example both methodTwo and methodThree in the below example will be protected.
class SampleClass
def methodOne
end
protected
def methodTwo
end
def methodThree
end
end
Private Methods
Private in Ruby means the method is private to an instance of the Object. What this means is that if you mark a method as private that method still exists for classes that inherit off the object but an instance of one object can not call the private method of another instance. This varies from Java in that private methods are not available in sub classes.
Another item to remember is that private methods can only be called implicitly and not by specifying the receiver.
class SampleClass
def methodA
end
private :methodA # defined methodA as private
def callMethodA
methodA # method call successfully
self.methodA # exception, you can not call a private method by defining the instance calling it even if it is self
obj.methodA # exception an instance can not access the private method of another object even if it is of the same class
end
end
As you can see private methods in Ruby are inheritable by sub classes but can only be called in the context of the calling instance.
Protected Methods
Protected methods in Ruby share the same inheritability of Private methods. However protected methods can be accessed explicitly by other instances of the object.
class SampleClass
def test( obj)
self.var1 = obj.myTestMethod
end
protected
def myTestMethod
"hello"
end
end
Pulling it all together
To make Method Visibility easier to understand one must understand that both private and protected methods are inherited by sub classes. The difference simply being that private methods can only be called in the context of an instance without defining an explicit caller of self. On the other hand protected methods are accessible from instances of the same class or inheriting sub classes.
Access Control, Method Visibility, My Self-Inflicted crash course introduction into Ruby, private, protected, public, Ruby