Skip to content

Instantly share code, notes, and snippets.

@gmodarelli
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmodarelli/4c2804d72be07263b02d to your computer and use it in GitHub Desktop.
Save gmodarelli/4c2804d72be07263b02d to your computer and use it in GitHub Desktop.
Source Code Reading
define_method(abbr) { |date = nil| DatePart.new(abbr, date) }
names = [ 'Giuseppe', 'Giulia', 'Luca', 'Andrea', 'Roberto' ]
grouped_by_first_letter = names.group_by { |name| name[0] }
puts grouped_by_first_letter
# >> {"G"=>["Giuseppe", "Giulia"], "L"=>["Luca"], "A"=>["Andrea"], "R"=>["Roberto"]}
module MyModule
chars = %w(A B C)
words = chars.permutation.to_a
words.each do |word|
method_name = word.join
module_eval(<<-PERMUTATION, __FILE__, __LINE__)
def #{method_name}
puts "#{word}"
end
PERMUTATION
end
end
include MyModule
abc # => nil
# >> a
# >> b
# >> c
chars = %w(A B C)
words = chars.permutation.to_a
# => [["A", "B", "C"],
# ["A", "C", "B"],
# ["B", "A", "C"],
# ["B", "C", "A"],
# ["C", "A", "B"],
# ["C", "B", "A"]]
chars = %w(A B C)
words = chars.permutation(2).to_a
# => [["A", "B"], ["A", "C"], ["B", "A"], ["B", "C"], ["C", "A"], ["C", "B"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment