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
4 + 4 # g8 | |
4 + "foo" # results in a TypeError, hence strongly typed | |
4 + 4.0 # => 8.0 this works because of type coercion | |
# What about this though? | |
def add_stuff | |
4 + "foo" | |
end # => nil | |
add_stuff # => TypeError This proves that Ruby is dynamically typed |
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
i = 0 | |
a = ['100', 100.0, '50', 50.0] | |
while i < a.size | |
<b>puts a[i].to_i</b> | |
i = i + 1 | |
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
foo = 'hello, plcourse' # here we define a function. Notice there's no type declaration | |
foo = 'is mutable' # variables are mutable, i.e. they can vary | |
CONSTANTS = 'are defined like this' # they are immutable |
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(foo) # this prints "hello, plcourse" and returns nil | |
puts foo # this is exactly the same as above, with a bit of syntactic sugar |
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 be_truthful | |
42 | |
true # The last expression is always the one that gets returned | |
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
foo = false or true # This expression evaluates to true | |
# => true | |
foo # But it assigned foo to false | |
# => 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
[1, "two", 3] # is perfectly valid | |
Array.new # remember, these are objects, and objects can be instantiated | |
# with .new() | |
cars = ['ford', 'toyota', 'subaru'] | |
cars[0] # => "ford" | |
cars[2] # => "subaru" | |
cars[-1] # => "subaru" | |
cars[-4] # => nil | |
cars[9001] # => 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
# Literal Form | |
numbers = { 1 => 'one', 2 => 'two' } | |
# => {1=>"one", 2=>"two"} | |
# Accessing element using key | |
numbers[1] # => "one" | |
# Can use symbols as keys | |
stuff = { :array => [1, 2, 3], :string => 'Hello' } | |
# => {:array=>[1, 2, 3], :string=>"Hello"} |
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 tell_the_truth(options={}) | |
if options[:profession] == :lawyer | |
'it could be believed that this is almost certainly not false.' | |
else | |
true | |
end | |
end | |
tell_the_truth | |
# => true |
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 Fixnum | |
def my_times | |
i = self | |
while i > 0 | |
i -= 1 | |
yield | |
end | |
end | |
end |