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
| class GameCharacter | |
| def initialize(name, type, weapons) | |
| @name = name | |
| @type = type | |
| @weapons = weapons | |
| end | |
| attr_accessor :name, :type, :weapons | |
| end |
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
| module RandomPassword | |
| def generate | |
| # create a one big array of seeding data | |
| seed = [('a'..'z'), ('!'..'+'), (1..9), ('A'..'Z')].map { |e| e.to_a }.flatten | |
| # get random 16 characters from this array | |
| original = (0..16).map { seed[rand(seed.length)] }.join | |
| # just to be sure, randomize them once more | |
| original.split('').shuffle.join | |
| end | |
| end |
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
| module A | |
| def our_method | |
| puts "Module A" | |
| end | |
| end | |
| class Including | |
| # by including a module we mark its methods as instance methods | |
| # that can only be used from the object scope | |
| include A |
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
| long = <<EOF | |
| THIS IS A VERY LONG STRING | |
| WITH MULTIPLE LINES, AND OTHER COOL STUFF | |
| LIKE INSERTING #{"Some cool string interpolation"} AND ALSO '' | |
| EOF | |
| puts long |
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
| # The lambda literal symbol -> | |
| # with it you can store a whole | |
| # anonymous method in a variable | |
| # and access it later | |
| # Introduced recently | |
| # The new default way to define scopes in rails | |
| one_plus_one = -> { 1 + 1 } | |
| p one_plus_one.call | |
| ############################################### | |
| expon = -> (v) { v ** v } |
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
| # facebook.com/ruby.dose | |
| # This is one of the coolest join tricks ruby can provide you... | |
| # creating an array of string literals, multiplying this array by another | |
| # string literal will actually cause joining this array together using | |
| # the latter string literal | |
| puts %w|Mohamad Abdullah Abdulmotaleb| * ' Ibn ' | |
| puts %w{Ahmad Hamdi Saad Emara} * ' Ibn ' |
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
| class Fixnum | |
| def digits_count | |
| # a cool trick to return the base 10 logarithm of x. | |
| # which always defaults to the actual number of digits - 1 | |
| Math.log10(self).to_i + 1 # here we add the missing 1. | |
| end | |
| end | |
| # a single random number | |
| item = rand(9999999) | |
| # just a dummy array |
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 is a regular parameter, the only first parameter | |
| # *b is whatever comes after 'a' grouped in an array | |
| # **c is any named: :argument | |
| def my_method(a, *b, **c) | |
| p a, b, c | |
| end | |
| my_method(3, 5, 7, 9, atom: 'is a text editor', ruby: 'is awesome') |
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
| class User | |
| attr_accessor :a, :b, :c | |
| end | |
| # basically the tap method yields the calling object | |
| # to the block and returns it back | |
| user = User.new.tap do |u| | |
| u.a = 1 | |
| u.b = 2 | |
| u.c = 3 |
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
| module OS | |
| def windows? | |
| # use !object.nil? instead of object != nil | |
| !(/mingw|mswin|cygwin|bccwin|emx/ =~ RUBY_PLATFORM).nil? | |
| end | |
| def mac? | |
| # check for the darwin kernel name | |
| !(/darwin/ =~ RUBY_PLATFORM).nil? | |
| end |