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….
- 01 Jul 2020
- 06 Apr 2020
- 11 Mar 2020
- 15 Feb 2020
- 02 Jan 2020
Recent on Build5Nines.com
Latest Cloud News: Azure Dev Guide eBook, xCloud, Cloud Shell + VNet and more! (August 11, 2020 – Build5Nines Weekly)11 Aug 2020
- 07 Aug 2020
Latest Cloud News: Azure Cloud Shell Tips, Azure Sphere, Microsoft acquire TikTok, NVidia acquire ARM, and more! (August 6, 2020 – Build5Nines Weekly)06 Aug 2020
- 01 Aug 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.