Lambda And Scopes

Lambda allows you to have that test at the end to make sure that the code executed in the lambda block doesn’t change change the User count.

Lambda in ruby is also a instance of Proc class.

Lambdas are a different flavor of procs.

Proc objects are blocks of code that have been bound to a set of local variables.

Sample Code :

=> def n_times(thing)
 lambda {|n| thing * n }
 end
 p1 = n_times(23)
 p1.call(3) # => 69
 p1.call(4) # => 92
 p2 = n_times(“Hello “)
 p2.call(3) # => “Hello Hello Hello “

Sample Example :

# Block Examples
=> [1,2,3].each { |x| puts x*2 } # block is in between the curly braces 
 
=> [1,2,3].each do |x| 
 puts x*2 # block is everything between the do and end 
 end
 
# Proc Examples
=> p = Proc.new { |x| puts x*2 } 
 [1,2,3].each(&p) # The ‘&’ tells ruby to turn the proc into a block 
 
=> proc = Proc.new { puts “Hello World” }
 proc.call # The body of the Proc object gets executed when called

# Lambda Examples
=> lam = lambda { |x| puts x*2 } 
 [1,2,3].each(&lam)

=> lam = lambda { puts “Hello World” } 
 lam.call

Differences between Procs and Lambdas

1. Lambdas check the number of arguments, while procs do not.

=> lam = lambda { |x| puts x } # creates a lambda that takes 1 argument 
 lam.call(2) # prints out 2 
 lam.call # ArgumentError: wrong number of arguments (0 for 1) 
 lam.call(1,2,3) # ArgumentError: wrong number of arguments (3 for 1)

In contrast, procs don’t care if they are passed the wrong number of arguments.

=> proc = Proc.new { |x| puts x } # creates a proc that takes 1 argument
 proc.call(2) # prints out 2
 proc.call # returns nil
 proc.call(1,2,3) # prints out 1 and forgets about the extra arguments

2. Lambdas and procs treat the ‘return’ keyword differently.

‘return’ inside of a lambda triggers the code right outside of the lambda code.

=> def lambda_test 
 lam = lambda { return }
 lam.call 
 puts “Hello world”
 end
 lambda_test # calling lambda_test prints ‘Hello World’

3. Lambdas and procs treat the ‘return’ keyword differently.

‘return’ inside of a lambda triggers the code right outside of the lambda code.

=> def lambda_test 
 lam = lambda { return }
 lam.call 
 puts “Hello world”
 end
 lambda_test # calling lambda_test prints ‘Hello World’

Scopes :

Scoping allows you to specify commonly-used queries which can be referenced as method calls on the association objects or models.

With these scopes, you can use every method previously covered such as where, joins and includes.

All scope methods will return an ActiveRecord::Relation object which will allow for further methods (such as other scopes) to be called on it.

Sample Example :

=> class Article < ApplicationRecord
 scope :published, -> { where(published:true) }
 end

This is exactly the same as defining a class method.

=> class Article < ApplicationRecord
 def self.published
 where(published: true)
 end
 end

Passing in arguments :

Your scope can take arguments:

=> class Article < ApplicationRecord
 scope :created_before, ->(time) { where(“created_at < ?”, time) }
 end

This is same as class method :
=> class Article < ApplicationRecord
 def self.created_before(time)
 where(“created_at < ?”, time)
 end
 end

Using conditionals :

Your scope can utilize conditionals:

=> class Article < ApplicationRecord
 scope :created_before, ->(time) { where(“created_at < ?”,time) if time.present? }
 end

Like the other examples, this will behave similarly to a class method.

=> class Article < ApplicationRecord
 def self.created_before(time)
 where(“created_at < ?”, time) if time.present?
 end
 end

Cryptex Technologies

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.