General Ruby Flashcards
General Ruby
check whether a value would be included in a given range
cover?(obj) => true or false
ex
(0. .100).cover? 50 #=> true
(0. .100).cover? 50.5 #=> true
General Ruby
quotes used with string interpolation
”
double quote
General Ruby
ternary syntax
expression ? true_value : false_value
General Ruby
nameless methods that are passed to methods as a parameter
blocks
General Ruby
object that is a block of code that can be bound to a set of local variables
proc
General Ruby
anonymous functions that can be saved to variables
lambdas
General Ruby
lambda syntax
prnt = lambda { |string| puts string }
prnt = -> (string) { puts string }
prnt.call (“my string”)
> my string => nil
General Ruby
lambdas compared to procs
stricter argument passing and localized returns
General Ruby
operator to signify when you are passing Procs/Lambdas/Methods as a block
&
General Ruby
case
syntax
case
when expression1
value1
when expression2
value2
else
default
end
General Ruby
easy way to create an array of strings (with interpolation)
%W(one two three four) => [“one”, “two”, “three”, “four”]
General Ruby
easy way to create a string without needing to escape quotation marks or line breaks (with interpolation)
%Q(John said, “My name is John”) => “John said, "My name is John"”
General Ruby
easy way to create an array of symbols (with interpolation)
%I(array of symbols #{1+1}) => [:array, :of, :symbols, :”2”]
General Ruby
difference between double quote and single quote strings
double quotes allow for interpolation and escape sequences (\n, \t, etc.), single quote does not allow for interpolation or escape sequences other than single quotes
General Ruby
proc syntax
Proc.new { |…| block } => a proc
General Ruby
gem that provides an alternative to irb
pry
General Ruby
change the scope of the current pry session
cd
ex
i = 1
cd i
General Ruby
list all of the methods available to a given object in a pry session
ls
ex
i = 1
ls i
ex
cd i
ls
General Ruby
display more information about a method in the current scope in a pry session
show-method method_name
General Ruby
allow a shell program to execute a script using the Ruby interpreter
! /usr/bin/env ruby
hash bang
ex
General Ruby
hash bang
ex
allow a shell program to execute a script using the Ruby interpreter
General Ruby
HEREDOC syntax to remove leading whitespace from each line
<<~HEREDOC
String text here, leading whitespace
will be stripped.
HEREDOC
General Ruby
<<~HEREDOC
String text here, leading whitespace
will be stripped.
HEREDOC
HEREDOC syntax to remove leading whitespace from each line
General Ruby
HEREDOC syntax that keeps whitespace intact
<<-HEREDOC
String text here, leading whitespace
will remain intact
HEREDOC