Skip to content

Instantly share code, notes, and snippets.

@Bivek
Last active April 1, 2022 05:10
Show Gist options
  • Save Bivek/ba93331b578137b598fce9ef729081d8 to your computer and use it in GitHub Desktop.
Save Bivek/ba93331b578137b598fce9ef729081d8 to your computer and use it in GitHub Desktop.
Ruby 101
1.next # => 2
next_method_object = 1.method("next")
next_method_object.call # => 2
arr = ['Ruby', 'Golang', 'Node.js', 'Deno', 'Typescript', 'Scala', 'Python']
arr.[](3) # => Deno
def add(*numbers)
numbers.inject(0) { |sum, number| sum + number }
end
puts add(1)
puts add(1, 2)
puts add(1, 2, 3)
puts add(1, 2, 3, 4)
def add(a_number, another_number, yet_another_number)
a_number + another_number + yet_another_number
end
numbers_to_add = [1, 2, 3] # Without a splat, this is just one parameter
puts add(*numbers_to_add) # Try removing the splat just to see what happens
def add(a_number, another_number, options = {})
sum = a_number + another_number
sum = sum.abs if options[:absolute]
sum = sum.round(options[:precision]) if options[:round]
sum
end
puts add(1.0134, -5.568)
puts add(1.0134, -5.568, absolute: true)
puts add(1.0134, -5.568, absolute: true, round: true, precision: 2)
l = lambda do |string|
if string == "try"
return "There's no such thing"
else
return "Do or do not."
end
end
puts l.call("try") # Feel free to experiment with this
Lambdas vs. Blocks
A lambda is a piece of code that you can store in a variable, and is an object. The simplest explanation for a block is that it is a piece of code that can't be stored in a variable and isn't an object. It is, as a consequence, significantly faster than a lambda, but not as versatile and also one of the rare instances where Ruby's "everything is an object" rule is broken.
def demonstrate_block(number)
yield(number)
end
puts demonstrate_block(1) { |number| number + 1 }
p $stdin.object_id
p STDIN.object_id
p $stdin.fileno
p STDIN.fileno
puts
p $stdout.object_id
p STDOUT.object_id
p $stdout.fileno
p STDOUT.fileno
puts
p $stderr.object_id
p STDERR.object_id
p $stderr.fileno
p STDERR.fileno
capture = StringIO.new
$stderr = capture
f = File.open('sample.txt', 'w')
$stdout = f
puts 'Now the content would be written to file'
def calculation(a, b, operation)
operation.call(a, b)
end
puts calculation(5, 6, lambda { |a, b| a + b }) # addition
puts calculation(5, 6, lambda { |a, b| a - b })
def calculation(a, b)
yield(a, b)
end
puts calculation(5, 6) { |a, b| a + b } # addition
puts calculation(5, 6) { |a, b| a - b } # subtraction
def filter(array, block)
return array.select(&block)
end
filter([1, 2, 3, 4], lambda {|number| number.even? }) returns [2, 4] ✔
filter([1, 2.0, 3, 4.0], lambda {|number| number.integer? }) returns [1, 3] ✔
Filter = lambda do |array, &block|
array.select(&block)
end
Filter.call([1, 2, 3, 4]) {|number| number.even? } returns [2, 4] ✔
Filter.call([1, 2.0, 3, 4.0]) {|number| number.integer? } returns [1, 3] ✔
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment