Skip to content

Instantly share code, notes, and snippets.

@gmodarelli
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save gmodarelli/4c2804d72be07263b02d to your computer and use it in GitHub Desktop.

Select an option

Save gmodarelli/4c2804d72be07263b02d to your computer and use it in GitHub Desktop.

Revisions

  1. gmodarelli revised this gist Nov 13, 2014. 1 changed file with 19 additions and 4 deletions.
    23 changes: 19 additions & 4 deletions module_eval.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,20 @@
    module_eval(<<-YMD, __FILE__, __LINE__)
    def #{name}(date = nil)
    #{set[0]} << #{set[1]} << #{set[2]}(date)
    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
    YMD
    end

    include MyModule

    abc # => nil
    # >> a
    # >> b
    # >> c
  2. gmodarelli revised this gist Nov 6, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions module_eval.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    module_eval(<<-YMD, __FILE__, __LINE__)
    def #{name}(date = nil)
    #{set[0]} << #{set[1]} << #{set[2]}(date)
    end
    YMD
  3. gmodarelli revised this gist Nov 6, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions words_generator_2.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    chars = %w(A B C)
    words = chars.permutation(2).to_a
    # => [["A", "B"], ["A", "C"], ["B", "A"], ["B", "C"], ["C", "A"], ["C", "B"]]
  4. gmodarelli revised this gist Nov 6, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions words_generator.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    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"]]
  5. gmodarelli revised this gist Nov 6, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions group_by.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    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"]}
  6. gmodarelli created this gist Nov 6, 2014.
    1 change: 1 addition & 0 deletions default_value.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    define_method(abbr) { |date = nil| DatePart.new(abbr, date) }