This file contains 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
def foo(a = "hello", b = "stranger") | |
puts "#{a} #{b}" | |
end | |
foo("go" "away") |
This file contains 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
# << operator | |
original = "foo" | |
copy = original | |
copy << "bar" | |
copy # => "foobar" | |
original # => "foobar" | |
# += operator | |
original = "foo" |
This file contains 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
# How to check how many arguments the method accepts? | |
class Foo | |
def initialize | |
end | |
def bar(a,b) | |
This file contains 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
# Check if class inherits from antoher class | |
if Fixnum < Numeric | |
puts "Fixnum is a numeric" | |
else | |
puts "Fixnum is not a numeric" | |
end |
This file contains 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
require 'yaml' | |
puts "Want some magic?" | |
y = YAML::load(DATA) | |
puts y.inspect | |
__END__ | |
--- |
This file contains 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, *b = [1,2,3,4] | |
# a = 1 | |
# b = [2,3,4] |
This file contains 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 String | |
def to_proc | |
eval "Proc.new { |*args| args.first#{self} }" | |
end | |
end | |
[1,2,3].collect(&'+5') |
This file contains 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
puts "Before block comment" | |
=begin | |
The method above prints a string to stdout | |
Pretty awesome! | |
And the method below does the same! | |
=end | |
puts "After block comment" |
This file contains 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
s = 'michal@example.com' | |
s[/^([^@]+)@(.*)/, 1] # => michal | |
s[/^([^@]+)@(.*)/, 2] # => example.com | |
s.match(/^([^@]+)@(.*)/)[1] # => michal | |
s.match(/^([^@]+)@(.*)/)[2] # => example.com | |
This file contains 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 Utils | |
def foo | |
puts "bar" | |
end | |
def bar | |
puts "foo" | |
end |