Skip to content
This repository was archived by the owner on May 23, 2020. It is now read-only.

Commit 8f72d93

Browse files
symbols section added
1 parent 0e55576 commit 8f72d93

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

README.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,4 +292,73 @@ _.sort!_ accepts a block which can be used to sort array in descending order as
292292
```ruby
293293
books.sort! { |firstBook, secondBook| firstBook <=> secondBook } #for ascending order
294294
books.sort! { |secondBook, firstBook| firstBook <=> secondBook } #for descending order
295-
```
295+
```
296+
297+
## Hashes and Symbols
298+
In Hashes, if you are trying to access a key which do not exist, Ruby return __nil__. It is Ruby's way of saying nothing at all. __=>__ is called the **hash rocket** symbol.
299+
300+
Hashes can also be defined using _symbols_:
301+
```ruby
302+
menagerie = { :foxes => 2,
303+
:giraffe => 1,
304+
:weezards => 17,
305+
:elves => 1,
306+
:canaries => 4,
307+
:ham => 1
308+
}
309+
```
310+
**_Note_**: Hash lookup is faster with symbol keys than with string keys.
311+
312+
Symbols are not strings. There can be different strings all having the same value, there's only one copy of any particular symbol at a given time. Symbols always start with a colon (:). They must be valid Ruby variable names. These are mainly used as hash keys or for referencing method names.
313+
```ruby
314+
"string == :string #false
315+
```
316+
317+
The **.object_id** method gets the ID of an object. It's how Ruby knows whether two objects are the exact same object.
318+
```ruby
319+
puts "string".object_id
320+
puts :symbol.object_id
321+
```
322+
323+
_.to_s_ and _.to_sym_ can be used for interconversion between strings and symbols.
324+
```ruby
325+
:hellothere.to_s
326+
# ==> "hellothere"
327+
328+
"hellothere".to_sym
329+
# ==> :hellothere
330+
```
331+
__.inter__ works the same as _.to_sym_.
332+
333+
In Ruby 1.9, the hash definition syntax changed.
334+
```ruby
335+
new_hash = {
336+
one: 1,
337+
two: 2,
338+
three: 3
339+
}
340+
```
341+
The keys are still symbols.
342+
343+
__.select__ method can be used on hashes for filtering out data based on some condition.
344+
```ruby
345+
grades = { rita: 100,
346+
mita: 97,
347+
ayush: 56,
348+
jane: 83
349+
}
350+
351+
grades.select { |name, grade| grade <= 97 }
352+
# ==> { :rita => 100, :mita => 97 }
353+
```
354+
355+
Every time in .each block for hashes we have to define variables for key as well value. Ruby has two methods __.each_key__ and __.each_value__ to overcome this problem.
356+
```ruby
357+
my_hash = { one: 1, two: 2, three: 3 }
358+
359+
my_hash.each_key { |k| print k, " " }
360+
# ==> one two three
361+
362+
my_hash.each_value { |v| print v, " " }
363+
# ==> 1 2 3
364+
```

string_vs_symbols.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require 'benchmark'
2+
3+
string_AZ = Hash[("a".."z").to_a.zip((1..26).to_a)]
4+
symbol_AZ = Hash[(:a..:z).to_a.zip((1..26).to_a)]
5+
6+
string_time = Benchmark.realtime do
7+
100_000.times { string_AZ["r"] }
8+
end
9+
10+
symbol_time = Benchmark.realtime do
11+
100_000.times { symbol_AZ[:r] }
12+
end
13+
14+
puts "String time: #{string_time} seconds."
15+
puts "Symbol time: #{symbol_time} seconds."

0 commit comments

Comments
 (0)