Ruby Flashcards
*.rb
Ruby code file extension
irb
built-in Ruby function for running ruby code outside a ruby code file.
#
ruby comment line
snake_case
defining or initializing variables, methods, or files, should always be named using snake_case:
lower case lettering and separated words using underscore.
constants
denoted by all uppercase letters
CamelCase
convention for class names Capitalize every word, no spaces
Max line length
80 chars
spaces around operators
use spaces around operators
- except exponent (4**2), the slash in rational literals (1/48r), safe navigation (foo&.bar)
- no spaces after ( and [, or before ] and )
- use spaces around { and before }, except interpolated expressions have no spaces
- no space after bang (!)
- no space inside range literals (‘a’…‘z’)
case when indents
case when.... puts.... when... puts... else .... end
puts
display result to console, adding line return at the end
puts “This is test to display”
puts my_variable
display result to console, with no trailing spaces or line feed
print “This is test to display”
print my_variable
text
Single line comment
=begin
text
more text
=end
multiline comment
Float
Integer
Numeric data types
\+ - * / ** %
Common arithmetic operations
variable.method
all variables in ruby are objects, and have methods available to them
strings
delineated by single or double quotation marks
true, false
Boolean values
.upcase
.downcase
.
converts string to all uppercase or lowercase
“your result is #{result}, congratulations”
string interpolation with variable
gets.chomp
method for getting user input
gets is the method for retrieving input
chomp removes the new line that ruby automatically adds to a gets call
if *test* do something elsif *test* do something else else do a third thing end
if else conditional
else is required, any number of elsif statements allowed
requires end to stop