I have a ruby code that currently look like this:
grades.sum{|g| g.grade * g.weight}
For a user maintained DSL, I would like to implement something like this:
grades.sum('grade' * 'weight')
Is this possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it’s not possible unless you monkeypatch the class
Stringto add your application logic (please don’t do that). But this -which even looks beter- is possible if you play a little bit withinstance_eval:For example, let’s assume
gradesis an array: