C# ? Operator in Ruby

C# ?? operator in Ruby?

You're looking for conditional assignment:

a ||= b  # Assign if a isn't already set

and the || operator

a = b || 2 # Assign if b is assigned, or assign 2

Ruby's equivalent to C#'s ?? operator

The name of the operator is the null-coalescing operator. The original blog post I linked to that covered the differences in null coalescing between languages has been taken down. A newer comparison between C# and Ruby null coalescing can be found here.

In short, you can use ||, as in:

a_or_b = (a || b)

What does mean both the operators * and (*) in ruby?

This is due to how parentheses work with parallel assignment as explained by Matz.

For example:

a, b, c = *[1, 2, 3]
a => 1
b => 2
c => 3

Is different than:

a, (b, c) = *[1, 2, 3]
a => 1
b => 2
c => nil

Basically, the parenthesis say: assign the right hand element at this index to the variables in the parens. So 2 is assigned to b, with nothing left at index 1 to assign to c. Similarly, (*) will take only the element at the given index and distribute it.

# the * is interpreted to mean 'take all remaining elements'
a, * = 1, 2, 3, 4

# the * is interpreted to mean 'take all remaining elements except
# the last element'
a, *, c = 1, 2, 3, 4

# incorrect syntax, can't splat more than once on all remaining
# elements
a, *, *, c = 1, 2, 3, 4

# the * is interpreted to mean 'take all elements at index 1'
a, (*), c = 1, 2, 3, 4

# the *'s are interpreted to mean 'take all elements at index 1,
# then again at index 2'
a, (*), (*), c = 1, 2, 3, 4

Typically, the * operator is used in conjuction with a variable as *foo - but if not it will hold its place and take element assignments as if it were a variable (essentially discarding them)

Operators as method parameters in C#

Well, in simple terms you can just use a lambda:

public void DoSomething(Func<int, int, int> op)
{
Console.WriteLine(op(5, 2));
}

DoSomething((x, y) => x + y);
DoSomething((x, y) => x * y);
// etc

That's not very exciting though. It would be nice to have all those delegates prebuilt for us. Of course you could do this with a static class:

public static class Operator<T>
{
public static readonly Func<T, T, T> Plus;
public static readonly Func<T, T, T> Minus;
// etc

static Operator()
{
// Build the delegates using expression trees, probably
}
}

Indeed, Marc Gravell has done something very similar in MiscUtil, if you want to look. You could then call:

DoSomething(Operator<int>.Plus);

It's not exactly pretty, but it's the closest that's supported at the moment, I believe.

I'm afraid I really don't understand the Ruby stuff, so I can't comment on that...

When do we use the ||= operator in Rails ? What is its significance?

Lets break it down:

@_current_user ||= {SOMETHING}

This is saying, set @_current_user to {SOMETHING} if it is nil, false, or undefined. Otherwise set it to @_current_user, or in other words, do nothing. An expanded form:

@_current_user || @_current_user = {SOMETHING}

Ok, now onto the right side.

session[:current_user_id] &&
User.find(session[:current_user_id])

You usually see && with boolean only values, however in Ruby you don't have to do that. The trick here is that if session[:current_user_id] is not nil, and User.find(session[:current_user_id]) is not nil, the expression will evaluate to User.find(session[:current_user_id]) otherwise nil.

So putting it all together in pseudo code:

if defined? @_current_user && @_current_user
@_current_user = @_current_user
else
if session[:current_user_id] && User.find(session[:current_user_id])
@_current_user = User.find(session[:current_user_id])
else
@_current_user = nil
end
end

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;
}
}

Ruby-like 'unless' for C#?

What about :

if (i<=5) i++;

if (!(i>5)) i++; would work too.


Hint : There is no unless exact equivalent.

implementation of operator overload (+=) in ruby

There's a more Ruby way of doing this that fixes the issue:

class Vector
attr_accessor :vector

def initialize(*values)
@vector = values
end

def [](i)
@vector[i]
end

def +(vector)
Vector.new(
*@vector.map.with_index do |v, i|
v + vector[i].to_i
end
)
end
end

Note that + is just a regular method, there's no +@ method involved here, that's the unary + as in r = +v, but you want to return a new object so you can chain it, as in a + b + c, and never modify the original.

+= creates a new object and does the assignment, as in x = x + 1.

In practice:

v = Vector.new(1,2)

r = v + Vector.new(2,3)

# => #<Vector:0x00007fc6678955a0 @vector=[3, 5]>

String literal without need to escape backslash

There is somewhat similar thing available in Ruby. E.g.

foo = %Q(The integer division operator in VisualBASIC is written "a \\ b" and #{'interpolation' + ' works'})

You can also interpolate strings in it. The only caveat is, you would still need to escape \ character.

HTH

Can you pass an 'expanded' array to a function in C# like in ruby?

Your method can be declared to accept a parameter array, via params:

void F(params int[] foo) {
// Do something with foo.
}

Now you can either pass a arbitrary number of ints to the method, or an array of int. But given a fixed method declaration, you cannot expand an array on-the-fly as you can in Ruby, because the parameters are handled differently under the hood (I believe this is different in Ruby), and because C# isn’t quite as dynamic.

Theoretically, you can use reflection to call the method to achieve the same effect, though (reflected method calls always accept a parameter array).



Related Topics



Leave a reply



Submit