Last active
January 19, 2017 23:22
-
-
Save oogali/9f532a6afadb148e79d2 to your computer and use it in GitHub Desktop.
The quick and lazy way to learn Ruby basics (assuming you have background in another similar language)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Ruby is present by default on all Mac OS X systems | |
| # It may or may not be default on Linux/BSD systems, but you can always install it via packages/ports | |
| # Ruby 1.8, 1.9, or 2.0 will work, so don't worry about having the right version | |
| # The Interactive Ruby Interpreter (irb) is what you'll be using for these examples | |
| # Let's get started | |
| oogali@marvin$ irb | |
| irb(main):001:0> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # a blank array | |
| > x = [] | |
| => [] | |
| # an array with 3 numbers | |
| > x = [3, 4, 5] | |
| => [3, 4, 5] | |
| # get the size of the array | |
| > x.count | |
| => 3 | |
| # add a new member to end of the array | |
| > x << 6 | |
| => [3, 4, 5, 6] | |
| # add a new member to beginning of the array | |
| > x.unshift(2) | |
| => [2, 3, 4, 5, 6] | |
| # get the first member of the array | |
| > x.first | |
| => 2 | |
| # get the last member of the array | |
| > x.last | |
| => 6 | |
| # remove and return the first member of the array | |
| > x.shift | |
| => 2 | |
| > x | |
| => [3, 4, 5, 6] | |
| # remove and return the last member of the array | |
| > x.pop | |
| => 6 | |
| > x | |
| => [3, 4, 5] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # an empty hash | |
| > x = {} | |
| => {} | |
| # set a key-value pair | |
| > x['key'] = 'value' | |
| => "value" | |
| > x | |
| => {"key"=>"value"} | |
| # get the value of key 'key' | |
| > x['key'] | |
| => "value" | |
| # get the value of non-existant key 'nope' | |
| > x['nope'] | |
| => nil | |
| # do we have a key named 'key'? | |
| > x.key?('key') | |
| => true | |
| # do we have a key named 'omg'? | |
| > x.key?('omg') | |
| => false | |
| # set the value of key 'wtf' to 'bbq' ONLY if key 'wtf' doesn't exist | |
| > x['wtf'] ||= 'bbq' | |
| => "bbq" | |
| # set the value of key 'key' to 'jacked' ONLY if key 'key' doesn't exist | |
| > x['key'] ||= 'jacked' | |
| => "value" | |
| # get an array of all the keys in our hash | |
| > x.keys | |
| => ["key", "wtf"] | |
| # get an array of all the values in our hash | |
| > x.values | |
| => ["value", "bbq"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # a blank string | |
| x = '' | |
| => "" | |
| # hello world | |
| > x = 'hello world' | |
| => "hello world" | |
| # append goodbye to our string | |
| > x + ', goodbye.' | |
| => "hello world, goodbye." | |
| # append goodbye to our string AND store the result in our variable | |
| > x += ', goodbye.' | |
| => "hello world, goodbye." | |
| > x | |
| => "hello world, goodbye." | |
| # does our string start with hello? | |
| > x.start_with? 'hello' | |
| => true | |
| # does our string start with jack bauer? | |
| > x.start_with? 'jack bauer' | |
| => false | |
| # does our string end with goodbye? | |
| > x.end_with? 'goodbye' | |
| => false | |
| # does our string end with goodbye and a period? | |
| > x.end_with? 'goodbye.' | |
| => true | |
| # does our string contain hell? | |
| > x.include? 'hell' | |
| => true | |
| # what is the first character? | |
| > x[0] | |
| => "h" | |
| # what is the last character? | |
| > x[-1] | |
| => "." | |
| # what are the *fifth* through *eighth* characters? | |
| > x[4..7] | |
| => "o wo" | |
| # how long is our string? | |
| > x.length | |
| => 21 | |
| # what is our string... backwards? | |
| > x.reverse | |
| => ".eybdoog ,dlrow olleh" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # create a basic string | |
| > x = 'The quick brown fox jumped over the lazy dog' | |
| => "The quick brown fox jumped over the lazy dog" | |
| # NOTE: everyone in the world loves Perl's regex syntax | |
| # so much so, that someone developed a library called libpcre (perl-compatible regular expressions) | |
| # and every modern language implements libpcre in order to make regex matching easier on the programmer | |
| # does our string match the pcre pattern /lazy/? | |
| > x =~ /lazy/ | |
| => 36 | |
| # previous regex pattern returned the starting position of the match, let's use it | |
| > x[36] | |
| => "l" | |
| # oh, what if get the text of the starting position up until the end of the string | |
| > x[36..-1] | |
| => "lazy dog" | |
| # does our string match the pcre pattern /doh/? | |
| > x =~ /doh/ | |
| => nil | |
| # let's use the .match method on the string | |
| > x.match /lazy/ | |
| => #<MatchData "lazy"> | |
| > x.match /doh/ | |
| => nil | |
| # again, let's use match and put it into a variable | |
| > m = x.match /lazy/ | |
| => #<MatchData "lazy"> | |
| # m is an array, that contains the data that matched | |
| > m[0] | |
| => "lazy" | |
| # let's capture data | |
| > m = x.match /lazy (\w+)/ | |
| => #<MatchData "lazy dog" 1:"dog"> | |
| # as in our first example, .match returns an array | |
| # 0 always contains the subject data | |
| # 1 and greater contain the data you matched in parentheses | |
| > m[0] | |
| => "lazy dog" | |
| > m[1] | |
| => "dog" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # create a range of numbers from 1 to 10 | |
| x = 1..10 | |
| > 1..10 | |
| # loop through our range of numbers, and print them to the screen | |
| > for num in x | |
| > puts num | |
| > end | |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| => 1..10 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment