Basic Ruby Commands Flashcards

You may prefer our related Brainscape-certified flashcards:
0
Q

How do you access the method new for the class Post?

A

Post.new

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

What command opens an interactive Ruby session?

A

There is more than one, you can use the standard interactive ruby irb command, or you can use pry. You can also use rails console to open an irb session.

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

How do you define a new method?

A

def sum(var1, var2)

** var1 + var2**

end

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

How do you write a symbol in ruby?

A

:var

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

Specify three debugging methods that will show you the fields of an object.

A

**<%= debug @objname %> **This will give you a formatted string using the yaml method.

**<%= simple_format @objname.to_yaml %> **Essentially the same as above.

<%= [1, 2, 3].inspect %> This will return unformatted text, but is useful for inspecting arrays.

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

Name three different methods for debugging a Ruby on Rails app.

A
  1. Debugging methods such as debug, and inspect.
  2. Using the logger to send information to a log file.
  3. Use the debugger gem to set breakpoints in the code and step through the code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do you use the debugger gem to insert a breakpoint in the code?

A

Use the debugger call on the line you want to break at. Make sure you’ve included debugger in the gem file and are running the rails server with the debugger option.

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

What are the two key priciples that Ruby is designed around?

A
  1. DRY: Don’t repeat yourself
  2. Convention over configuration
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you set the root page for your website?

A

Open config/routes.rb and add the line root ‘controller#action’. In the case of the welcome controller it would be root ‘welcome#index’

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

What is rake?

A

Ruby Make, a make system built for and using Ruby.

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

How do you add a link to a page in Ruby on Rails?

A

<%= link_to ‘Link Name’, path %>

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

How could you display the value of a variable in the console?

A

With the puts command. E.g

puts var

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

What is the simplest way to concatenate 2 strings?

A

catstring = string1 + string2

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

How can you run a Ruby script from the command line?

A
  1. change to the files directory and type ruby script.rb
  2. Use the shebang method and place #! /usr/local/bin/ruby -w at the top of the file, and make the file executable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the Ruby documentation system called?

A

RDoc

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

How can you access the Ruby documentation from the command line?

A

ri class

or for specific methods use

ri class::method

16
Q

How do you define and access an array?

A

a = [1, 2, 3] or a = %w(1 2 3)

a[2] returns 3

17
Q

How can you insert a variable or call into a string?

A

**x = ‘This is a string #{x.inspect}’ **

18
Q

Describe a hash and how do you define one?

A

A hash is a type of array that contains pairs of keys and values, where the keys are unique.

h = {‘GT’ => ‘Ford’,

‘DBS’ => ‘Aston Martin’}

or using symbols

h = {one: ‘first’,

two: ‘second’}

19
Q

Write an if statment

A

if x > y

elseif

else

end

20
Q

Write a while loop

A

while x < y

x += 1

end

21
Q

Describe how you can pass parameters to a ruby program run from the command line

A

ruby program.rb var1 var2

in the program code the input variables can be access through the ARGV array