# Update the grade of each student to a value 1.2 times the previous
value.
students.each { |s| s.grade *= 1.2 }
# Note that this turns the grade from an Integer into a Float
#To find the student with highest grade
students.max_by &:grade
#To sort in grade order:
students.sort_by! &:grade
Since students contains an array of objects, the proper way would be
students.map { |student| student.grade * 1,2 }
Well there is one thing you should note. the use of functions with ! and
without !. So map will return an array of all the students grade time 1.2.
The students array will have continue to have the array of students and not
the individual grade.
so you will need to store the returned (array of grades time 1.2) into
another variable. If you want to use it later.
···
On Wed, Oct 9, 2013 at 3:56 PM, Anders Friis <lists@ruby-forum.com> wrote:
Sorry, Harisankar. I editted my last reply, because I figured it out my
self. But you are such a huge help!
Last question:
If I want to multiply each grade by 1.2 . How can I do that?
Now doing this will return an array of students grade multiplied by 1.2,
but it would also change the original value of grade, as (stduent.gradge *=
1.2 means student.grade = students.grade * 1.2, and thus we are assigning
this value to the grade attribute)
···
On Wed, Oct 9, 2013 at 4:15 PM, Anders Friis <lists@ruby-forum.com> wrote:
Isn't there a smart way, to update just the grades and still have the
names in the away?
I just don't understand all the letters in
the {}. When to use a, when to use b, when to use s etc.
Usually a single-letter variable is just a temporary one. The general
trend is for people to use a,b,c for objects, and i,j,k for "counting".
Personally I tend to use a shorthand signifier in simple loops, like "s"
for student; and full names for anything with multiple variables to
avoid confusion.
Anders Friis wrote in post #1124003:
One last thing. Is there a way to sort ind the array, so you only show
students with 50 or above in grade?
Array#reject or Array#select would be most suitable for this, rather
than sorting. If you wanted to output only names which a grade of 50 or
more:
I'm about to understand this. I just don't understand all the letters in
the {}. When to use a, when to use b, when to use s etc.
Variables a, b, are not chosen based any rule. Its just a random variable
name. When you are working with only one element in mind you use, just |a|
like in the case of map etc. Some functions allows two variables, in those
case u use a, b
One last thing. Is there a way to sort ind the array, so you only show
students with 50 or above in grade?
I should have send you the link to the ruby documentation, it would have
helped you solve a lot of doubts.
The function required for your purpose is called reject.
array.reject { |elemen| if element < 50}
regards,
···
On Wed, Oct 9, 2013 at 5:42 PM, Anders Friis <lists@ruby-forum.com> wrote:
***WARNING***
This e-mail message is for the sole use of the intended recipient(s) and
may contain confidential and privileged information. Any unauthorized
review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply e-mail and destroy
all copies of the original message. THANK YOU.
On Thu, Oct 10, 2013 at 7:29 AM, Anders Friis <lists@ruby-forum.com> wrote:
Regards
Iftikhar Barrie
// Sent Via BlackBerry® Wireless //
// Envoyé sans fil par mon terminal mobile BlackBerry® //
This email and any attachments are confidential. Re-distribution is prohibited. If this message was received in error please destroy it and any attachments and notify the sender immediately.