Skip to content

Instantly share code, notes, and snippets.

View BitOfUniverse's full-sized avatar

Alexey Nikolaev BitOfUniverse

View GitHub Profile
@BitOfUniverse
BitOfUniverse / trigger_on_change_in_react.js
Created March 14, 2018 17:22
Send onChange event in React
// in React after 15.6
const triggerInputChange = (node, value = '') => {
const inputTypes = [
window.HTMLInputElement,
window.HTMLSelectElement,
window.HTMLTextAreaElement
];
if (inputTypes.indexOf(node.__proto__.constructor) > -1) {
@BitOfUniverse
BitOfUniverse / return_multiple_values.rb
Created March 28, 2019 12:07
Ruby return multiple values
status, reason = send def authorize
response = do_request
if response.success?
return :ok, response.body
else
return :error, response.error
end
end
@BitOfUniverse
BitOfUniverse / iffe2.rb
Created March 28, 2019 11:55
Ruby IFFE with method definition
send def execute_right_now
puts "That's nice!"
end
@BitOfUniverse
BitOfUniverse / iffe3.rb
Created March 28, 2019 11:56
Ruby IFFE as a method parameter
some_method def another_method; end
@BitOfUniverse
BitOfUniverse / iffe4.rb
Created March 28, 2019 11:56
Ruby IFFE usage in Rails
class RailsController
helper_method def current_user
User.current
end
end
@BitOfUniverse
BitOfUniverse / iffe_plus_finally.rb
Last active March 28, 2019 12:07
Ruby continue method execution
send def escape
return "I want to return"
ensure
puts "print this text first"
end
# Output looks like this:
print this text first
=> "I want to return"
@BitOfUniverse
BitOfUniverse / iffe_plus_finally_plus_return.rb
Created March 28, 2019 12:05
Ruby continue method execution with two return statements
send def escape
return "I want to return"
ensure
return "I want to finally get out"
end
=> "I want to finally get out"
@BitOfUniverse
BitOfUniverse / return_multiple_values_example.rb
Created March 28, 2019 12:08
Ruby return multiple values example
return [:ok, response.body]
# or
[:ok, response.body]
@BitOfUniverse
BitOfUniverse / infinite_lambda_self_invocation.rb
Created March 28, 2019 12:10
Ruby infinite lambda self invocation
go_deeper.call.call.call.call.call.call.call.call
# You can try to implement it yourself. Your code will look like this:
(go_deeper = -> { puts "deeper"; go_deeper }).call.call.call.call.call
@BitOfUniverse
BitOfUniverse / nested_hash.rb
Created March 28, 2019 12:12
Ruby nested hash
h = Hash.new {|h, k| h[k] = Hash.new }