Last active
February 3, 2017 14:23
-
-
Save kalorz/50c4118502fe4a862340bc08c50773b3 to your computer and use it in GitHub Desktop.
Ruby Gotchas
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
surprise = true and false # => surprise is true | |
surprise = true && false # => surprise is false |
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
(surprise = true) and false # => surprise is true | |
surprise = (true && false) # => surprise is false |
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
'foo'.upcase! # => "FOO" | |
'FOO'.upcase! # => nil |
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
@name.upcase! and render :show |
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
@name.upcase! | |
render :show |
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
1 == 1.0 # => true | |
1.eql? 1.0 # => false |
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 MyException < Exception | |
end | |
begin | |
raise MyException | |
rescue | |
puts 'Caught it!' | |
end | |
# MyException: MyException | |
# from (irb):17 | |
# from /Users/karol/.rbenv/versions/2.1.0/bin/irb:11:in `<main>' |
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
MY_SCOPE = 'Global' | |
module Foo | |
MY_SCOPE = 'Foo Module' | |
class Bar | |
def scope1 | |
puts MY_SCOPE | |
end | |
end | |
end | |
class Foo::Bar | |
def scope2 | |
puts MY_SCOPE | |
end | |
end | |
Foo::Bar.new.scope1 # => "Foo Module" | |
Foo::Bar.new.scope2 # => "Global" |
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 Foo | |
class Bar | |
end | |
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
class Foo | |
private | |
def self.bar | |
puts 'Not-so-private class method called' | |
end | |
end | |
Foo.bar # => "Not-so-private class method called" |
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 Foo | |
class << self | |
private | |
def bar | |
puts 'Class method called' | |
end | |
end | |
def self.baz | |
puts 'Another class method called' | |
end | |
private_class_method :baz | |
end | |
Foo.bar # => NoMethodError: private method `bar' called for Foo:Class | |
Foo.baz # => NoMethodError: private method `baz' called for Foo:Class |
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 Foo | |
def self.bar=(value) | |
@foo = value | |
return 'OK' | |
end | |
end | |
Foo.bar = 3 # => 3 |
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 'Assigned' if (Foo.bar = 3) == 'OK' # => nil |
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 Foo | |
def show | |
puts 'Foo#show' | |
end | |
end | |
class Bar < Foo | |
def show(text) | |
super | |
puts text | |
end | |
end | |
Bar.new.show('test') # ArgumentError: wrong number of arguments (1 for 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment