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.
Isn’t there a smart way, to update just the grades and still have the
names in the away?
try this
students.map { |student| student.grade *= 1.2 }
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)
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.
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 F. 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:
Hi
Where Can I find a good download site for Ruby for Microsoft Windows?
Any
suggestions?
Regards,
Iftikhar Barrie
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.
-------------------------------- 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.