Class in array

Hi all!

First of all. I'm new to Ruby. I am working on a program in school -
find the highest grade for X amount of students.

I've made this code:

class Person
  def initialize(name, grade)
    @name = name
    @grade = grade
  end
end

## Main
students = Array.new

  st1 = Person.new("Mathias", 57)
  students<<st1

  st2 = Person.new("Marta", 32)
  students<<st2

  st3 = Person.new("Peter", 24)
  students<<st3

The grades are stored in the students array. But is there a way to
compare these grades?

Thank you!

···

--
Posted via http://www.ruby-forum.com/.

That makes kinda sense :slight_smile:

If i try running this:

puts students.last.inspect

My terminal returns:

#<Person:0x007f8548849840 @name="Mathias Abbot", @grade=57>

That is is almost perfect. How can i take the 57 integer and store it in
a variable outside the array?

···

--
Posted via http://www.ruby-forum.com/.

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?

Is is something like:

students.map {|grade| grade * 1.2}

···

--
Posted via http://www.ruby-forum.com/.

Isn't there a smart way, to update just the grades and still have the
names in the away?

···

--
Posted via http://www.ruby-forum.com/.

class Person
  attr_accessor :grade, :name
  def initialize(name, grade)
    @name = name
    @grade = grade
  end
end

# One-liner:
students = [ Person.new("Mathias", 57), Person.new("Marta", 32),
Person.new("Peter", 24) ]

# 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

···

--
Posted via http://www.ruby-forum.com/.

Thank you guys!

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.

One last thing. Is there a way to sort ind the array, so you only show
students with 50 or above in grade?

···

--
Posted via http://www.ruby-forum.com/.

I've read in the Ruby Doc. But i'm not good enough to understand it :slight_smile:

When i'm going to print out the final result. Why is this not working?

students.each do |name, grade|
  print name + " , " + grade
  print "\n"
end

···

--
Posted via http://www.ruby-forum.com/.

The grades are stored in the students array. But is there a way to
compare these grades?

Yes you can do that using sorry function of the array.

students.sort! { |a, b| a.grade <=> b.grade }

But for the above snippet to work you need to make the
grade, accessible outside your class.

class Person

     attr_accessor :grade

  def initialize(name, grade)
    @name = name
    @grade = grade
  end
end

Happy coding :slight_smile:

···

On Wed, Oct 9, 2013 at 2:49 PM, Anders Friis <lists@ruby-forum.com> wrote:

--
Harisankar P S

Thank you very much to both of you! :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Thank you so much, Harisankar!

students.sort! { |a, b| a.grade <=> b.grade }

Can you please explain me, what the quoted code does? Is it comparing
two grades (a.grade & b.grade)?

···

--
Posted via http://www.ruby-forum.com/\.

<=> : this is called a combined comparison operator.

When a and b are equal it returns 0
When a is greater than b it returns 1
when b is greater than a it return -1

Based on these values, array sort function sorts, each object.

···

On Wed, Oct 9, 2013 at 3:12 PM, Anders Friis <lists@ruby-forum.com> wrote:

Thank you so much, Harisankar!

> students.sort! { |a, b| a.grade <=> b.grade }

--
Harisankar P S

Well if you want to sort and also get the highest scorer then i would say

     highest_score = students.sort! { |a,b| a <=> b}.last.grade

now the students array will have the sorted grades and the higest_score
would store the maximum score attained.

If you don't want sort the array but just find the maximum grade. then i
suggest you use the array max function.

     highest_score = students.max { |a| a.grade}.grade

···

On Wed, Oct 9, 2013 at 3:25 PM, Anders Friis <lists@ruby-forum.com> wrote:

#<Person:0x007f8548849840 @name="Mathias Abbot", @grade=57>

That is is almost perfect. How can i take the 57 integer and store it in
a variable outside the array?

--
Harisankar P S

You are almost correct.

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?

Is is something like:

students.map {|grade| grade * 1.2}

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

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)

···

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?

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

Anders Friis wrote in post #1124003:

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:

students.select { |s| s.grade >= 50 }.each { |s| puts s.name, s.grade }

Or, a shorter way, outputting only the ones which match:

students.each { |s| puts s.name, s.grade if s.grade >= 50 }

···

--
Posted via http://www.ruby-forum.com/\.

Thank you guys!

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:

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

I've read in the Ruby Doc. But i'm not good enough to understand it :slight_smile:

When i'm going to print out the final result. Why is this not working?

students.each do |name, grade|
  print name + " , " + grade
  print "\n"
end

The way your visualizing is wrong. students is an array, an array of object

[object, object, object]

so when you apply .each, it will access only one element at a time. The
attributes grade and name are with in those objects.

so to print the code u need to write as bellow

students.each do |student| #referring to the single student object with

in the array
  puts "#{student.name] has grade #{student.grade}"
end

Since you are printing an integer along with a string, you can't use the +
operator. You need to write in the above format.

"#{variable_name}" is the way ruby let is easily place a variable in a
string.

···

On Wed, Oct 9, 2013 at 6:26 PM, Anders Friis <lists@ruby-forum.com> wrote:

--
Harisankar P S
https://twitter.com/coder_hsps | http://tech.hsps.in

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.

On Thu, Oct 10, 2013 at 7:29 AM, Anders Friis <lists@ruby-forum.com> wrote:

Thank you very much to both of you! :slight_smile:

--
Posted via http://www.ruby-forum.com/\.

Ifthikhar Barrie wrote in post #1124140:

Hi
Where Can I find a good download site for Ruby for Microsoft Windows?
Any
suggestions?

http://lmgtfy.com/?q=ruby+windows&l=1

···

--
Posted via http://www.ruby-forum.com/\.

Thanks

···

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.

From: Joel Pearson

Sent: Thursday, October 10, 2013 7:54 AM

To: ruby-talk@ruby-lang.org

Reply To: ruby-talk@ruby-lang.org

Subject: Re: Class in array

Ifthikhar Barrie wrote in post #1124140:

Hi
Where Can I find a good download site for Ruby for Microsoft Windows?
Any
suggestions?

http://lmgtfy.com/?q=ruby+windows&l=1


Posted via http://www.ruby-forum.com/.