Skip to content

Struct and ENV (CH 3.2.2-3.2.3)

2023-09-14

Discussion Points

  1. Why is the mode (100644) defined when it is for every files?
  2. Interesting discovery on what Struct does in Ruby vs in C
  3. Getter and Setter
  4. Object.methods.sort
  5. Hash vs ENV object
  6. $stdin vs STDIN

Why is the mode (100644) defined when it is for every files?

The string 100644 that each tree entry should begin with.

A mode of 100644, which means it’s a normal file.

Other options are 100755, which means it’s an executable file; and 120000, which specifies a symbolic link. The mode is taken from normal UNIX modes but is much less flexible — these three modes are the only ones that are valid for files (blobs) in Git (although other modes are used for directories and submodules).

Chmod 644 (chmod a+rwx,u-x,g-wx,o-wx) sets permissions so that, (U)ser / owner can read, can write and can't execute. (G)roup can read, can't write and can't execute. (O)thers can read, can't write and can't execute.

A more extensive explanation can be found in Section 5.1.1, “File modes”.

Interesting discovery on what Struct does in Ruby vs in C

In several programming languages, particularly C, structures (also called structs) are a way to group several related variables into one place.

In Ruby, we see that Struct is more useful. The Struct class generates new subclasses that hold a set of members and their values.

# author.rb
Author = Struct.new(:name, :email, :time) do
  def to_s
    timestamp = time.strftime("%s %z")
    "#{ name } <#{ email }> #{ timestamp }"
  end
end

Getter and Setter

We saw that initializing a new Author object has methods such as :name and :name=.

A name like :name= is typically called a setter method. The :name= method corresponds to the attribute name.

This will automatically call the setter method sending whatever is after the equal sign = to the method as a parameter.

Source: https://shaqqour.medium.com/how-to-use-getters-and-setters-in-ruby-edb24f722970

.methods.sort

.methods to see the methods an object has access to. .methods.sort to sort these methods.

Hash vs ENV object

Excerpt:

# jit.rb
  tree = Tree.new(entries)
  database.store(tree)

  name    = ENV.fetch("GIT_AUTHOR_NAME")
  email   = ENV.fetch("GIT_AUTHOR_EMAIL")
  author  = Author.new(name, email, Time.now)
  message = $stdin.read

  commit = Commit.new(tree.oid, author, message)
  database.store(commit)

We saw ENV is like a Hash, but the class is Object.

[6] pry(main)> hash = {"name"=>"daphne"} 
=> {"name"=>"daphne"}
[7] pry(main)> hash.class
=> Hash
[8] pry(main)> ENV.class
=> Object
[9] pry(main)> ENV.fetch("USER")
=> "daphne"
[10] pry(main)> hash.fetch_values("name")
=> ["daphne"]

ENV is a hash-like accessor for environment variables.. The ENV object interacts with the operating system's environment variables.

$stdin vs STDIN

https://stackoverflow.com/questions/4279604/what-is-the-difference-between-stdin-and-stdin-in-ruby

Ruby has two ways of referring to the standard input: The STDIN constant, and the $stdin global variable.

In your own code you should use $stdin to be consistent with the built-in methods².

Edited by Mike Lockhart | GitLab