What is the -> (dash greater than) operator in Ruby/Rails [duplicate] - ruby-on-rails

This question already has answers here:
What is the -> (stab) operator in Ruby? [duplicate]
(1 answer)
What does -> mean in Ruby [duplicate]
(2 answers)
Closed 9 years ago.
I just ran across the following line of code in a Rails app:
scope :for_uid, ->(external_id) { where(external_id: external_id) }
What does the -> operator mean? It's kind of hard to Google.

This is syntactic sugar.
->(external_id) { where(external_id: external_id) }
is equal to:
lambda { |external_id| where(external_id: external_id) }

It's new lambda notation. This syntax was introduced in ruby 1.9, and is used to define unnamed functions.
In your example it is scope defined by unnamed function.

The -> operator was introduced in Ruby 1.9 as a shorthand syntax for the old lambda function. It behaves nearly identically to the lambda function but allows you to specify parameters outside the block:
lambda {|param| puts param }
# becomes
-> (param) { puts params }

Related

How do I query a WhereChain in Rails 6?

I’m upgrading from Rails 4.2 to Rails 6. I have this scope in a model …
scope :valid_type, -> { where{ (model_type == nil) | (modeltype << [36, 38]) } }
Previously, I could run this
MyModel.valid_type.include?(model_instance)
But now I get
NoMethodError:
undefined method `include?' for #<ActiveRecord::QueryMethods::WhereChain:0x00007fb9fc58b3e0>
What’s the right way in Rails 6 to check for inclusion in a WhereChain?
You are currently passing a block to where. where does not accept a block neither in 4.2 or 6.0
The block parameter is being ignored. Since you're not passing an argument to where, it's being treated as a WhereChain that only has one method on it: not. For example as a part of where.not(my_column: nil)
You'll need to fix your syntax. Use the .or method to join your queries and use proper ActiveRecord syntax.
scope :valid_type, -> { where(model_type: nil).or(where(modeltype: [36, 38])) }
In your current case though, Rails will handle the nil for you so there is no need for the or
scope :valid_type, -> { where(modeltype: [nil, 36, 38]) }

Parameter passing in Swift [duplicate]

This question already has answers here:
When are argument labels required in Swift?
(6 answers)
Closed 6 years ago.
I have the following piece of code written in swift:
func hai(greeting: String, times: Int) -> String {
return "You are greeted + \(greeting) + \(times) times "
}
hai ("Hello", times: 3)
When I call the function hai, if I call it the following way hai("hello", 3) it throws an error and forces me to call the way it was mentioned above.
Can someone please explain why this should be the case? Thanks, I am new to IOS programming.
Each function parameter in Swift has two names - an internal and an external one. When you define function signature the way you did, the external parameter name of times is the same as its internal name. You can tell Swift that you do not want an external name by placing _ in the external name position:
func hai(greeting: String, _ times: Int) -> String
// ^
Learn more about internal/external parameter names in the Swift 2.2 Programming Language Guide.

The contextual keyword 'var' may only appear within a local variable declaration Or In Script Code [duplicate]

This question already has answers here:
Why can't class fields be var? [duplicate]
(4 answers)
Closed 6 years ago.
Hey I'm trying to Define a mock database . I'm encountering an error while trying to equate var to Mock<'Repository'> The error is :
The contextual keyword 'var' may only appear within a local variable declaration Or In Script Code.
The Code that I have written is :
public class MockingDatabse
{
//Mock a Payment Info
var newPayment = new Mock<IPayment>();
}
I know that I can replace 'var' with 'Mock<"Repository">'. But I wanna know I'm not able to use
'var'
Try this:
public class MockingDatabse
{
//Mock a Payment Info
Mock<IPayment> newPayment = new Mock<IPayment>();
}

Match returning straight input in all but one case [duplicate]

This question already has answers here:
How to map a parametrized enum from a generic type to another?
(4 answers)
Closed 7 years ago.
Let's say I have an enum like this
pub enum Status<T> {
Error,
NotStarted,
Incomplete,
Complete(T),
}
and I want to do this
match foo(x) {
Complete(i) => Complete(bar(i)),
Error => Error,
NotStarted => NotStarted,
Incomplete => Incomplete,
}
i.e. only do something with it, if it matched one "special" option and return match input unseen otherwise. In my code this happens a lot.
Is there another way to do this in a shorter way? Probably something like this:
match foo(x) {
Complete(i) => Complete(bar(i)),
_ => _,
}
Just bind the other cases to a name and return that
match foo(x) {
Complete(i) => Complete(bar(i)),
other => other,
}

Dart calling a member function by function name [duplicate]

This question already has answers here:
Dynamic class method invocation in Dart
(3 answers)
Closed 8 years ago.
I am wondering if there is anyway to call a function by its name in dart as in javascript.
I would like to do something as such:
foo["bar"]();
I don't want readers to think what the questioner wants isn't possible in Dart, so I'm adding an answer.
You need to use Mirrors to call a method if you have its name available as a string. Here is an example:
import 'dart:mirrors';
class Foo {
bar() => "bar";
}
void main() {
var foo = new Foo();
var mirror = reflect(foo);
print(mirror.invoke(#bar, []).reflectee); // Prints 'bar'.
}

Resources