Ruby Equivalent of C# 'using' Statement
>> Math::PI
=> 3.14159265358979
>> PI
NameError: uninitialized constant PI
from (irb):3
>> include Math
=> Object
>> PI
=> 3.14159265358979
OTOH, if the issue is just aliasing class names, consider that, as they say "Class is an object, and Object is a class".
So:
>> require 'csv'
>> r = CSV::Reader
>> r.parse 'what,ever' do |e| p e end
["what", "ever"]
Yes, in Ruby the class name is just a reference like any other to an object of class Class
.
ruby equivalent of c#'s internal keyword?
Unfortunately, Ruby is a language that's quite lacking in terms of encapsulation. It's capacity there are quite limited so, no, there isn't anything equivalent to C# internal keyword. Maybe in your case two separate classes would be better, but a clear docstring or a special naming scheme could do the same without heavy modifications.
Also, you must note that the internal keyword is nothing more than convenience for library developers. It doesn't truly prevent someone from using a method or class marked as internal, there's way to circumvent these protection with code injection, AOP, etc.
Ruby, variables and their C# equivalent
C# does not use sigils for variables.
The "equivalent" C# variable depends entirely on how the variable/member is defined. Note that there are differences even between the "equivalent" forms.
However, there are a number of naming conventions that are encouraged to be followed. The exact conventions used vary by project and may differ from the names I chose below, which reflect my conventions - do not use "class" or "instance" or "local" in real variable names.
Examples:
class MyClass: IMyInterface {
// "const" makes it constant, not the name
public const int CONSTANT = 42;
// static member variable - somewhat like Ruby's @@variable
private static int classVariable;
public static int ExposedClassVariable; // but use properties
// @variable - unlike Ruby, can be accessed outside "self" scope
int instanceVariable;
public int ExposedInstanceVariable; // but use properties
void method (int parameter) {
int localVariable;
}
}
C# does not have "global variables in a shared namespace", but static member variables can be accessed by a stable path which means they can be effectively abused as global variables.
C# equivalent of the Ruby symbol
In your case, sending a flag can be done by using an enum...
public enum Message
{
Connect,
Disconnect
}
public void Action(Message msg)
{
switch(msg)
{
case Message.Connect:
//do connect here
break;
case Message.Disconnect:
//disconnect
break;
default:
//Fail!
break;
}
}
C#'s LINQ for collection manipulation equivalent in ruby
The equivalent of where
of Linq in Ruby is find_all
Check the documentation for the Enumerable Module for other functions.
Equivalent of Ruby redo in C#
for (int i = 0; i < 100; i++)
{
do
{
DoYourStuff();
} while (ShouldWeDoThatAgain());
}
Do...while
is like a standard while
loop, except instead of checking its conditional before each iteration, it checks after. That way, the code inside the loop will always execute at least once. Stick that inside a for
or foreach
loop, and that should get you the behavior your want. This is a bit simpler than Simon's answer, as it doesn't require an extra variable, doesn't use continue
, and doesn't mess with the loop counter at all.
C# expression, equivalent to ruby's sandwich block code
You certainly don't need anything IoC-related here. How about:
public T ActOnFile<T>(string filename, Func<Stream, T> func)
{
using (Stream stream = File.OpenRead(stream))
{
return func(stream);
}
}
public int CountLines(string filename)
{
return ActOnFile(filename, stream =>
{
using (StreamReader reader = new StreamReader(stream))
{
int count = 0;
while (reader.ReadLine() != null)
{
count++;
}
return count;
}
});
}
In this case it doesn't help very much, as the using
statement already does most of what you want... but the general principle holds. Indeed, that's how LINQ is so flexible. If you haven't looked at LINQ yet, I strongly recommend that you do.
Here's the act CountLines
method I'd use:
public int CountLines(string filename)
{
return File.ReadLines(filename).Count();
}
Note that this will still only read a line at a time... but the Count
extension method acts on the returned sequence.
In .NET 3.5 it would be:
public int CountLines(string filename)
{
using (var reader = File.OpenText(filename))
{
int count = 0;
while (reader.ReadLine() != null)
{
count++;
}
return count;
}
}
... still pretty simple.
Related Topics
In C# Wpf, Why Is My Tabcontrol's Selectionchanged Event Firing Too Often
How to Open the Physical File Operating System Error 32
Deserialize JSON to Anonymous Object
Application Can't Scaffold Items
How to Convert Xml to JSON Using C#/Linq
Dynamically Select Columns in Runtime Using Entity Framework
Use Decimal Values as Attribute Params in C#
Returning a String from Pinvoke
Sort Collection by First Item of Sub List in Entity Framework
Entity Framework Initialization Is Slow -- What How to Do to Bootstrap It Faster
Duplicate Key Exception from Entity Framework
How to Generate Web Service Out of Wsdl
How Set Value a Property Selector Expression<Func<T,Tresult>>
Is There a Tool for Finding Unreferenced Functions (Dead, Obsolete Code) in a C# App