Why does the first example code of "What you'll be building" outputs 668 as erics password?


#1



Chapter 17, Excercise 1
https://www.codecademy.com/en/courses/ruby-beginner-en-X5wcR/0/1?curriculum_id=5059f8619189a5000201fbcb


I am just wondering why the code outputs the password 668 for eric, after we have set it to 01234?
I'm a little confused here, I expected 01234.


class Machine
  @@users = {}
  
  def initialize(username, password)
    @username = username
    @password = password
    @@users[username] = password
    @files = {}
  end
  
  def create(filename)
    time = Time.now
    @files[filename] = time
    puts "#{filename} was created by #{@username} at #{time}."
  end
  
  def Machine.get_users
    @@users
  end
end

my_machine = Machine.new("eric", 01234)
your_machine = Machine.new("you", 56789)

my_machine.create("groceries.txt")
your_machine.create("todo.txt")

puts "Users: #{Machine.get_users}"

outputs

groceries.txt was created by eric at 2016-09-26 20:22:23 +0000.
todo.txt was created by you at 2016-09-26 20:22:23 +0000.
Users: {"eric"=>668, "you"=>56789}
nil

on my machine.


#2

The leading 0 tells Ruby to parse this as an octal.

1234 base 8 == 668 base 10

#3

Wow! Thats something completely new to me. So

0x123456789ABCDEF => hexadecimal
1234567890 => decimal
01234567 => octal
0b0101110 => binary

if I understand this correctly?


#4

Looks that way. Good that you tracked down those examples.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.