Skip to content

Commit

Permalink
added clarification for dot notation in hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
gauthamchandra committed Sep 9, 2015
1 parent b9fa4b4 commit 70766e3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions primitives_and_other_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ a[:status] #Same as a['status']

Instead of using ```:``` to seperate key value pairs, In Ruby, one can also use ```=>``` instead.

**NOTE:** notice that the property names are in quotes. Ruby requires that they either be in quotes or be a [symbol](#symbols). Otherwise, Ruby assumes it is a variable or a function and would return an error.

```ruby
stuff = {
Expand Down Expand Up @@ -158,6 +159,31 @@ puts stuff[a] # prints out "3"
puts stuff[b] # prints out "0"
```

####Dot notation and Ruby

Unlike JavaScript where objects (i.e Ruby hashes) can be accessed by dot notation, Ruby forces hash properties to be accessed via *array notation*.

This is because dot notation indicates to Ruby that its a function:

```javascript
//in javascript
var foo = {
bar: 3
};

foo.bar //completely valid. => 3
```

```ruby
foo = {
'bar' => 3
};

foo['bar'] # Valid. => 3
foo.bar # Invalid. Looks for a function named bar that belongs to foo and throws an error.
```


##Strings

Strings works pretty much the same way in JS as it does in Ruby with a few nice enhancements.
Expand Down

0 comments on commit 70766e3

Please sign in to comment.