Search By Label
array = [1, 2, 3, 4] new_array = [] array.each do |element| new_array << element * 2 end new_array # => [2, 4, 6, 8]
each
handle and return the same array and for this reason, it is necessary to create a new_array
variable to inject the new values of the product to element * 2
map
:array = [1, 2, 3, 4] array.map do |element| element * 2 end # => [2, 4, 6, 8]
# Ensure the value is one or more [value, 1].min # Ensure the value is 100 or less [value, 100].max
# define a command class class AuthenticateUser prepend SimpleCommand # optional, initialize the command with some arguments def initialize(email, password) @email = email @password = password end # mandatory: define a #call method. its return value will be available # through #result def call if user = User.find_by(email: @email)&.authenticate(@password) ... end
AuthenticateUser.call(session_params[:email], session_params[:password])
def my_method(value:, second_value:) puts "value #{value}, second_value #{second_value}" end my_method(value: 'value one', second_value: 'value two')
module SimpleCommand def self.included(klass) klass.class_eval do def self.call(*args, **kwargs) puts "args #{args}" new(*args, **kwargs).call end end end end
module SimpleCommand def self.included(klass) klass.extend(ClassMethods) end module ClassMethods def call(*args, **kwargs) new(*args, **kwargs).call end end end
class NamedArguments include SimpleCommand def initialize(value: , second:) @value = value @second = second end def call puts "the method in myClass @value: #{@value}, @second #{@second}" end end
NamedArguments.call(value: 'the value', second: 'from second')
class SimpleArguments include SimpleCommand def initialize(valueOne, valueTwo) @valueOne = valueOne @valueTwo = valueTwo end def call puts "the method in myClass @value: #{@valueOne}, @second #{@valueTwo}" end end
SimpleArguments.call('arg one', 'arg two')
lambda = lambda {|value| value ** value }
lambda.(10)
lambda.call(10)
lambda === 10
lambda[10]
proc = Proc.new {|value| value ** value }
proc.(10)
proc.call(10)
proc === 10
proc[10]
(1..10).to_a.each.with_index do |element, index|
puts "element #{element}, index: #{index}"
break if index == 3
end
catch :label do
(1..10).to_a.each.with_index do |e, index|
throw :label if condition
end
end
def my_method result = [] catch :completed do (1..10).to_a.each.with_index do |element, index| result << element throw :completed if index == 3 end end result end my_method => [1, 2, 3, 4]
def return_example values = [] (1..10).to_a.each_with_index do |element, index| values << element return values if index == 3 end end
return_example => [1, 2, 3, 4]
[11,22,31,224,44].each_with_index { |val,index| puts "index: #{index} for #{val}" if val < 30} index: 0 for 11 index: 1 for 22 => [11, 22, 31, 224, 44]
" Hello world ".strip # => "Hello world"
# Define a class
class Animal
end# Create an object
dog = Animal.new# Check if the object is an instance of a class
puts dog.is_a?(Animal) # true
puts dog.is_a?(Object) # true, since all objects are instances of Object
puts dog.is_a?(String) # false, since a dog is not a String
text = "Hello, World! This is a test. Hello, Universe!" new_text =text.sub("Hello", "Hi") puts new_text # => "Hi, World! This is a test. Hello, Universe!"
"".present? # false (empty string) " ".present? # false (whitespace characters only) nil.present? # false (nil) "hello".present? # true (contains non-whitespace characters) [1, 2, 3].present? # true (array with elements)
"".blank? # true (empty string) " ".blank? # true (whitespace characters only) nil.blank? # true (nil) "hello".blank? # false (contains non-whitespace characters) [1, 2, 3].blank? # false (array with elements)
Enumerable#cycle
offers an easy way to either repeat a certain pattern n times or just to switch between two predefined states. irb> array = [1, 2, 3] => [1, 2, 3] irb> array.cycle(3).to_a => [1, 2, 3, 1, 2, 3, 1, 2, 3]
irb> button = ['on', 'off'].cycle => # irb> button.next => "on" irb> button.next => "off"