Recently, I finally got around to starting to learn Ruby. I am finding it to be rather pleasant to program in. Now, one thing that I am used to using with an Object Oriented language (especially with my .NET background) are Namespaces. The only problem is that you can't just declare a namespace in Ruby using a "namespace" keyword. However, it's not difficult to implement Namespaces in Ruby, all you need to do is nest some modules.
To demonstrate this, I'll show you how to create the familiar (for .NET developers) System.Windows.Forms namespace with the MessageBox.Show method:
module System module Windows module Forms module MessageBox def MessageBox.Show(title, message) api = Win32API.new("user32","MessageBox",["L","P","P","L"],"I") api.call(0, message, title, 0) end end end end end
Now to use the namespace you can use one of the following methods:
System::Windows::Forms::MessageBox.Show("Some Title", "This is a test message.")
Or by including the "namespace":
include System::Windows::Forms MessageBox.Show("Some Title", "This is another test message.")
As you can see it's rather simple to create ".NET"-like namespaces in Ruby.
Now, I know this technique is basically allowing me to write .NET code in Ruby, but that was pretty much the point of figuring this out. Now, back to learning how to write Ruby code in Ruby.
Oh, yeah now only if the syntax highlighter in BlogEngine.NET supported Ruby….
- 10 Oct 2020
- 01 Jul 2020
- 06 Apr 2020
- 11 Mar 2020
- 15 Feb 2020
Recent on Build5Nines.com
- 04 Dec 2020
Latest Cloud News: Apple on K8s, IoT, Microsoft Pluton and more! (November 20, 2020 Build5Nines Weekly)20 Nov 2020
Latest Cloud News: .NET 5 Released, Apple Silicon M1 CPU, and more! (November 12, 2020 Build5Nines Weekly)12 Nov 2020
- 11 Nov 2020
Latest Cloud News: Kubernetes, Terraform, Teams Multi-Login and more! (November 5, 2020 Build5Nines Weekly)05 Nov 2020
This site is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. We also participates in affiliate programs with Udemy, and other sites. This site is compensated for referring traffic and business to these companies.