Last active
December 10, 2015 08:58
-
-
Save daiando/0e4bf70137c558866667 to your computer and use it in GitHub Desktop.
[ ruby ] group_byを使う ref: http://qiita.com/penguin_dream/items/179460ee63f7ca26a988
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evaluate_num(val) | |
return "RED" if val <= 2 | |
return "BLUE" if val > 3 | |
end | |
(1..5).group_by {|x| | |
evaluate_num(x) | |
} | |
#=> {"RED"=>[1, 2], "BLUE"=>[3, 4, 5]} | |
#marks = {"Ramesh":23, "Vivek":40, "Harsh":88, "Mohammad":60} | |
def group_by_marks(marks, n) | |
def evaluate(age, standard) | |
return "Failed" if age < standard | |
return "Passed" if age >= standard | |
end | |
marks.group_by{|k, v| | |
evaluate(v, n) | |
} | |
end | |
##=> {"Failed"=>[["Ramesh", 23]], "Passed"=>[["Vivek", 40], ["Harsh", 88], ["Mohammad", 60]]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def evaluate_num(val) | |
return "RED" if val <= 2 | |
return "BLUE" if val > 2 | |
end | |
(1..5).group_by {|x| | |
evaluate_num(x) | |
} | |
=> {"RED"=>[1,2], "BLUE"=>[3, 4, 5]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1..5).group_by {|x| x%2} | |
=> {1=>[1,3,5], 0=>[2, 4]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment