General Ruby Flashcards

1
Q

General Ruby

check whether a value would be included in a given range

A

cover?(obj) => true or false

ex

(0. .100).cover? 50 #=> true
(0. .100).cover? 50.5 #=> true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

General Ruby

quotes used with string interpolation

A

double quote

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

General Ruby

ternary syntax

A

expression ? true_value : false_value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

General Ruby

nameless methods that are passed to methods as a parameter

A

blocks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

General Ruby

object that is a block of code that can be bound to a set of local variables

A

proc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

General Ruby

anonymous functions that can be saved to variables

A

lambdas

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

General Ruby

lambda syntax

A

prnt = lambda { |string| puts string }

prnt = -> (string) { puts string }

prnt.call (“my string”)

> my string => nil

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

General Ruby

lambdas compared to procs

A

stricter argument passing and localized returns

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

General Ruby

operator to signify when you are passing Procs/Lambdas/Methods as a block

A

&

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

General Ruby

case syntax

A

case

when expression1

value1

when expression2

value2

else

default

end

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

General Ruby

easy way to create an array of strings (with interpolation)

A

%W(one two three four) => [“one”, “two”, “three”, “four”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

General Ruby

easy way to create a string without needing to escape quotation marks or line breaks (with interpolation)

A

%Q(John said, “My name is John”) => “John said, "My name is John"”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

General Ruby

easy way to create an array of symbols (with interpolation)

A

%I(array of symbols #{1+1}) => [:array, :of, :symbols, :”2”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

General Ruby

difference between double quote and single quote strings

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

General Ruby

proc syntax

A

Proc.new { |…| block } => a proc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

General Ruby

gem that provides an alternative to irb

17
Q

General Ruby

change the scope of the current pry session

A

cd

ex

i = 1

cd i

18
Q

General Ruby

list all of the methods available to a given object in a pry session

A

ls

ex

i = 1

ls i

ex

cd i

ls

19
Q

General Ruby

display more information about a method in the current scope in a pry session

A

show-method method_name

20
Q

General Ruby

allow a shell program to execute a script using the Ruby interpreter

A

! /usr/bin/env ruby

hash bang

ex

21
Q

General Ruby

hash bang

ex

A

allow a shell program to execute a script using the Ruby interpreter

22
Q

General Ruby

HEREDOC syntax to remove leading whitespace from each line

A

<<~HEREDOC

String text here, leading whitespace

will be stripped.

HEREDOC

23
Q

General Ruby

<<~HEREDOC

String text here, leading whitespace

will be stripped.

HEREDOC

A

HEREDOC syntax to remove leading whitespace from each line

24
Q

General Ruby

HEREDOC syntax that keeps whitespace intact

A

<<-HEREDOC

String text here, leading whitespace

will remain intact

HEREDOC

25
# General Ruby \<\<-HEREDOC String text here, leading whitespace will remain intact HEREDOC
HEREDOC syntax that keeps whitespace intact
26
# General Ruby read a single character from stdin
getch must require 'io/console' ex IO::console.getch $stdin.getch STDIN.getch
27
# General Ruby getch must require 'io/console' ex IO::console.getch $stdin.getch STDIN.getch
read a single character from stdin
28
# General Ruby handle an exception that would otherwise cause your program to stop execution
begin...end ex begin something which might raise an exception rescue SomeExceptionClass =\> some\_variable code that deals with some exception rescue SomeOtherException =\> some\_other\_variable code that deals with some other exception else code that runs only if \*no\* exception was raised ensure ensure that this code always runs, no matter what end
29
# General Ruby a stream designed for use in scripts that process files given as command-line arguments or passed in via STDIN
ARGF ex ARGF.each do |line| puts line end
30
global object that has information the current and parent processes; behaves much like a hash
ENV
31
return the string resulting from applying format\_string to any additional arguments; within the format string, any characters other than format sequences are copied to the result
format(format\_string [, arguments...] ) =\> string
32
Kernel#format format\_string syntax
%[flags][width][.precision]type