Beginning Ruby for C# Developers

Ruby has become quite popular as a programming language over the past few years. Probably mainly due to the popularity of the Ruby on Rails framework. Because of its popularity, it makes sense to learn at least the basics of it and what all it can do for you as a developer.

Until I have a better solution for embedding Ruby code and output, I'm just embedding the code and results as Github Markdown for now. Even though it doesn't seem to display properly in the embed, it does when viewing the actual gist on GitHub.

Getting Started

One of the easiest ways to get started using Ruby is to just use your browser. Repl.it is a site that supports many dynamic languages to be run in a browser and it's very handy to just play around in.

I've also seen people use Sublime Text to edit their Ruby code (I admit, I enjoy using it as well as a light weight IDE. I've had no problems using the beta of version 3, either). For Windows, you'd just need to install Ruby. If you're on a Mac, you already have it installed. In fact, you can just type "ruby -v" in the terminal to see what version you have.

Since Ruby is a dynamic language, there is no compiling. However, everything in Ruby is considered an object, and because of that everything will include some built in functions with it. With everything in Ruby being an object isn't necessarily a new concept to C# developers since every object in C# derives from System.Object and you do get a few predefined functions, Ruby just seems to have gone that extra step. 

For example, say you want to run a statement three times in a row. How would you do that in Ruby? It's actually easier than just using a for-loop similar to what you would do in C# or even creating a way to use a lambda expression.

Pretty easy and very straight forward, especially for someone who may not even have a programming background.

Let's go on with a few other basics...

Output

Ruby has a couple of ways to display output back to the console. The main way is to use the "print" statement, though you may also see the "puts" (put string) command as well. The only difference in these is the "puts" command will append a new line. You'll probably see these statements quite a bit, especially if you go through online tutorials.

Methods

As mentioned earlier everything in Ruby is an object, and since that's true it can have methods on those objects. Of course, depending on the type Ruby recognizes it during runtime is what methods are available. For example, the "length" method can be used on strings but not on numbers.

Methods, like in C#, can also be chained together to give some added syntactic sugar and to reduce the overall code needed to accomplish what you need.

One final note about methods that got me confused early on but I thought was really nice is that there is a bit of a naming convention associated with certain methods. Mainly methods that change the object itself and methods that return a boolean.

Take the "upcase" method we used earlier. There are actually two versions of this method, "upcase" and "upcase!". The first returns a copy of the string being used, the second (indicated with an exclamation point) changes the instance of the string being used. In other words, the first version causes the string to be immutable.

For methods that return a boolean value a question mark is appended to the end of the method name to indicate to the developer that that method can be used for branching. Take the string's "empty?" method, for example. As you can probably tell, it returns if the string is empty or not.

Program Flow

Like most programming languages Ruby supports the if/else block. However, one noticeable difference is that Ruby also has an "unless"' keyword. It will only execute if the given conditional is false. This was included to help make certain branching more readable and a bit more intuitive.

So we can change the structure of an if/else to an unless block like so...

Further Learning

One of the best resources I can recommend to help get you on the path of learning Ruby much easier is Code Wars. This site has quite a few code katas that will help in understanding the syntax and included methods that come in with Ruby.

Codecademy is recommended a lot, as well, by people and for very good reason...they have a lot of good interactive tutorials for learning Ruby.

Conclusion

Of course, I definitely don't know everything about the language or all the tools the community uses. I still haven't gotten my head totally around gem files or rake. I'm not even totally sure of the correct structure of a Ruby application! But, hopefully this will at least spark some interest in you C# people to maybe try something different outside of the .NET world. There is plenty of fun to be had with it and I throughly enjoy learning the language and all it has to offer.

Core C#: Delegates

Microsoft has made it easier for aspiring developers to easily get started creating something when they came out with the C# language. However, there are definitely some advanced aspects of the language that take quite a bit longer than others to fully grasp. Delegates is one of those types of language features. Hopefully by the end of this article you'll have a deeper understanding of how utilize delegates in your C# code. 

I, myself, had trouble trying to fully understand what the purpose of delegates were. If you were to ask me before what a delegate was, I would just give a small answer indicating that it's similar to a method template. While that's true, it does quite a bit more.

The simplest way I know to go over delegates is to just go through a small example to allow you to also do the same so you can use that to experiment around with the code.

Let's go through this a bit. The first line is where you actually declare the delegate and give it a return type, name, and any parameters if it needs any. In our Main method is where we'll initiate a couple of variables to hold the delegates. The first one, myDelegate , does so using the method syntax. Our second variable, however, we declare it using an anonymous delegate, or a delegate without a function name.

After we've declared the our variables with our delegate, we can then invoke our delegate by calling it just like a method just like in lines 15 and 16 above. We declared our delegate to take in a single string parameter, so that's how we'll invoke it. 

Built in Delegate Types 

The folks at Microsoft were kind enough to give us a few built in delegates that we can also use within our code. Let's take a quick look at them. 

The first one, which may be the most commonly used, especially if you've messed quite a bit with the LINQ method syntax, is the Func  delegate type. This one will take in zero or more parameters and will return something back to the caller. In the example above I have one parameter of a string and I return back an integer.

The second one is the Action delegate type. Like the Func type it takes in zero or more parameters, however it returns void (returns nothing).

The third one, Predicate, is a bit closer to the Func type where it takes in parameters and returns. The difference with this is that it will always return a boolean true or false back to the caller.

Invocation Lists

One of the magic things that delegates have is they keep track of all invocations of it. When we instantiate our delegates we are actually adding to the invocation list. Another way to do this is to simply add a delegate to another delegate. 

We'll see the true power of delegates and their invocation lists in the next part, when we talk about events.

Consuming the Jamendo API with AngularJS: HTTP Get

After doing a small AngularJS introduction, I figured it's time to start diving into some specifics of an Single Page Application with it.

One of the awesome things developers can do is to play with other third party services. A common way to do that is with an API that is provided by the creators of the service to give developers the option to create all sorts of software to use or interact with their services.

Honestly, I had a class library created and ready to create a C# wrapper to make the API calls, but I thought that I should use more client side code instead of server side code as that tends to be more of how things are being done these days. I thought I'd use AngularJS, which I chose so I can learn more on how to use the framework and well as to get used to developing more with JavaScript.

NOTE: For the API calls to Jamendo I'm using a clientId that they provide for testing and demoing only. This is required to use the API and if you want to take full advantage of their API you will need to register in their Development Center.

For this first part I'll construct a simple artist search via $http from Angular's API to return a few items back from the Jamendo API for display.

The HTML

The HTML is a fairly similar setup as the previously mentioned introductionAnd please remember, I'm by all means no designer (though, I have been reading up on the subject) so I apologize in advance if it causes you to cringe. 

What we'll concentrate on is what all is in the body tag as the items in the head tag are just pulling in different resources. We tell the body tag to use our Album controller (we'll get further into that below). I then created a div tag that uses a class from one of our CSS files to move everything in there to the near center of our page. 

Now to the fun stuff. Inside our container we have a text input and a button. Our text box will be what artist we're searching for on Jamendo and our button will be used to conduct the call to get our results.

On our text box we have the ng-model directive named "artist". We'll see how this gets used later in our controller.

Our button has the ng-click directive which tells Angular to execute this method on our controller once the button is clicked.

The Album Controller

Once again, this is fairly similar to our Employees controller from our introduction. The differences are fairly small, however. One thing you may notice is when defining the controller function I bring in an extra parameter that AngularJS provides. Along with the $scope I bring in $http to be able to call another site. In the function I create another function called getAlbums. In here is where I actually make the call via the get command. You'll also notice that I'm using promises that is also provided by AngularJS.

Here's another part of where AngularJS's data binding comes into play. Remember the ng-model of "artist" we had in our text box? That was bound to the $scope and was used in the call to Jamendo. That's all we had to do.

Enabling CORS

You may have noticed that you haven't seen where I create the "jamendoApp" module in the controller. This is because, after some reading and viewing others code, I created a base app.js file that handles that as well as some config and routing settings that will be used throughout the whole application.

Also another thing to make note of is that, since I'm using AngularJS to make a third party call to get data most browsers may not allow the call to take place due to having to enable cross-origin resource sharing. If you tried to make the same call without enabling CORS you would get a same-origin policy error. This basically allows the browser to only access resources that are in the same domain as the page you're calling from. To get around this I got help from this StackOverflow question and implemented the configuration below. 

Demo

To demo how it all works GitHub makes it super easy to view HTML pages from our repositories using an HTML Preview. Simply go to this page to interact with it and feel free to come back and let me know what you think about it.

Full code to play with can be found on the GitHub repository. 

Next time we'll refactor the controller and have our API call from a AngularJS service.

YUI: The Other JavaScript Framework

When you think of JavaScript frameworks, what pops into your mind? JQuery, KnockoutJS, or EmberJS? Did you know that Yahoo! has one of their own? It's YUI  and it's actually not too bad of a framework. 

It's definitely not what you'd think of when creating a new web application. For any kind of DOM manipulation, I'm sure the first thought will be to use jQuery. I wouldn't exactly count out Yahoo!, though. They have some of the top engineers of the country and they released YUI as an open project for anyone to fork and contribute to.

Below is a very small example of using YUI. How easily can you read through it? 

Fairly simple, right?  You get one instance of the element with the "btn1" ID and on the click event run the function that has the alert.

Let's look at a more complex example of YUI in action. We're going to make a JSONP call to the GitHub API. Let's take a look.

Not too much more here than in our first example. I'm still using the selector and click event handler as we've seen but in the click handler of the button I make a JSONP call to the GitHub API. The real magic happens during the function that handles the response from the JSONP call. The Y.Lang.sub  call takes in an HTML template and uses the data from the response to bind the items in the brackets. Of course, the names have to match what you get back in the response.

Conclusion

These are just very small examples of what you can do with YUI. The documentation contains a ton more examples and functionality of what the framework is capable of.

I do believe this is easier to maintain and use than using jQuery. I do have to say, though, since I've found AngularJS, I believe I'll be using that more and more. If I couldn't use that, however, YUI would be my framework of choice. 

Small Introduction to AngularJS

I know what you're probably thinking, "Oh, another JavaScript library/framework for me to learn." Well, yes, that's correct. However, I believe this is one worth learning. 

Why this one over the many other ones available to you? AngularJS has a different approach. Instead of providing a library, it's goal is to make HTML dynamic like most modern web sites are instead of being the old, boring static HTML we're all used to.

Of course, when you first see AngularJS in action, it can look pretty weird. That can be said of a lot of libraries, frameworks, or technologies until you get used to it. I was the same thing when I first started messing with lambdas in .NET.

Below is a small example for AngularJS mostly showing controllers and some basic data binding.

At first, you may notice all the ng-* attributes in the HTML. This is all AngularJS and, as mentioned above, it extends HTML to be more dynamic. The double curly braces ( {{ }} ) tell Angular that it's a two way binding. Inside the braces is the name on the model to use. You can also tell that most of the functions and properties are in the EmployeeController JavaScript file, which we'll take a look at next.

The first line tells what application Angular will be using. This is the same as the ng-app  attribute in the HTML. The empty array will be for any dependencies we may need, such as additional JavaScript files. The next section is where we're defining the actual EmployeeController, which sets our initial Employee object and defines the add and remove methods. These methods get called in their appropriate places inside the HTML. You may notice we also pass in the $scope object into our controller function. This is mainly for AngularJS and can be used for dependency injection later on, if you need it.

Below is the full demo in action. Note that JSFiddle automatically puts in the html tag, so I had to wrap the body tag with the ng-app attribute for Angular to work correctly.

Additional Resources

The one tutorial that got me started on the path of Angular is Dan Wahlin's AngularJS in 60ish Minutes.

Another good tutorial is the one from Thinkster.  They combine a lot of the articles and tutorials from all over and give them all to you piece by piece along with their own videos.

Conclusion

This was just a very  small introduction to AngularJS. While there are tons of other things you can do, this should be good to get something up and going. Once a little bit is understood, the tutorials and all will make so much more sense and you will be able to learn a bit more than you did before. Hope you enjoy learning AngularJS as much as I have!

You Are Good Enough

"I don't think I'm a good developer." 

I think a lot of developers out there think this or something similar at one point in their career. Mainly during the first few years when they started out. Maybe they just started their first job or even thinking of moving to a second job.

Here's my advice if you're one of those who think this...just relax.

Yes, I definitely have done this myself. All through college, when interviewing for my first job, during my first job, interviewing for my second, and during the first couple of months of my second. Seems like a lot of times worrying and doubting yourself, doesn't it? 

It's kind of nice that you worry about this stuff. It means you actually care, right? But just think, if you weren't as good as you may think, how would you have gotten the job you do now? How would you have been able to stay there for as long as you have? 

If you still feel the same about your skill, take this opportunity to learn new stuff. The internet has tons of stuff online to get you started. One tutorial doesn't make sense? Go to the next one.

So go on, challenge yourself. After doing that you'll become better than good enough.