Hi, I just started learning about an hour a go and already need help. :-)

25 views
Skip to first unread message

Nick Savage

unread,
Sep 11, 2015, 11:07:22 AM9/11/15
to Ruby on Rails: Talk
Okay I started with Learn the hard way with ruby.
My code is below and below each code is my question.
I guess I do not understand the identifiers because when I do them in my head they dont add up.

Any help would be appreciative and the way I learn I can not move on till I know.

puts "I will now count my chickens:'"
puts "Hens #{25 + 30 / 6}"
#how does that equal 30?
puts "Roosters #{100 - 25 * 3 % 4}"
#How does this equal 97

puts "Now I will count the eggs:"

puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
#How does this equal 7, is it because it is 6.5 and it rounds up.


Rob Biedenharn

unread,
Sep 11, 2015, 11:23:41 AM9/11/15
to rubyonra...@googlegroups.com
On 2015-Sep-11, at 11:00 , Nick Savage <nick....@gmail.com> wrote:

Okay I started with Learn the hard way with ruby.
My code is below and below each code is my question.
I guess I do not understand the identifiers because when I do them in my head they dont add up.

Any help would be appreciative and the way I learn I can not move on till I know.

This is just barely a Ruby issue. Most operator precedence is the same as you'd expect just from math:



puts "I will now count my chickens:'"
puts "Hens #{25 + 30 / 6}"
#how does that equal 30?

25 + (30/6)
25 +    5
30

puts "Roosters #{100 - 25 * 3 % 4}"
#How does this equal 97

100 - (25 * 3 % 4)
100 - ((25 * 3) % 4)  # same precedence, do them left-to-right
100 - (    75   % 4)
100 - (         3  )  # 75 % 4 is the remainder after 75/4
100 - 3
97


puts "Now I will count the eggs:"

puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
#How does this equal 7, is it because it is 6.5 and it rounds up.

Here you have to add a fact (behavior) common to many programming languages: "integer division truncates to an integer"

3 + 2 + 1 - 5 + (4%2) - (1/4) + 6
3 + 2 + 1 - 5 + ( 0 ) - ( 0 ) + 6   # then do the + and - left-to-right
  5   + 1 - 5 + ( 0 ) - ( 0 ) + 6
      6   - 5 + ( 0 ) - ( 0 ) + 6
          1   + ( 0 ) - ( 0 ) + 6
              1       - ( 0 ) + 6
                      1       + 6
                              7

Very simple. Perhaps you're over-thinking it?

-Rob

Scott Ribe

unread,
Sep 11, 2015, 12:35:01 PM9/11/15
to rubyonra...@googlegroups.com, Nick Savage
On Sep 11, 2015, at 9:00 AM, Nick Savage <nick....@gmail.com> wrote:
>
> Any help would be appreciative and the way I learn I can not move on till I know.
>
> puts "I will now count my chickens:'"
> puts "Hens #{25 + 30 / 6}"
> #how does that equal 30?
> puts "Roosters #{100 - 25 * 3 % 4}"
> #How does this equal 97
>
> puts "Now I will count the eggs:"
>
> puts 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
> #How does this equal 7, is it because it is 6.5 and it rounds up.

Google "ruby operator precedence". Multiplication & division have higher precedence than addition & subtraction, is the answer to your immediate question. But you really should google and look at all the precedence categories.

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice





Reply all
Reply to author
Forward
0 new messages