Basic Ruby Commands Flashcards
How do you access the method new for the class Post?
Post.new
What command opens an interactive Ruby session?
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 do you define a new method?
def sum(var1, var2)
** var1 + var2**
end
How do you write a symbol in ruby?
:var
Specify three debugging methods that will show you the fields of an object.
**<%= 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.
Name three different methods for debugging a Ruby on Rails app.
- Debugging methods such as debug, and inspect.
- Using the logger to send information to a log file.
- Use the debugger gem to set breakpoints in the code and step through the code.
How do you use the debugger gem to insert a breakpoint in the code?
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.
What are the two key priciples that Ruby is designed around?
- DRY: Don’t repeat yourself
- Convention over configuration
How do you set the root page for your website?
Open config/routes.rb and add the line root ‘controller#action’. In the case of the welcome controller it would be root ‘welcome#index’
What is rake?
Ruby Make, a make system built for and using Ruby.
How do you add a link to a page in Ruby on Rails?
<%= link_to ‘Link Name’, path %>
How could you display the value of a variable in the console?
With the puts command. E.g
puts var
What is the simplest way to concatenate 2 strings?
catstring = string1 + string2
How can you run a Ruby script from the command line?
- change to the files directory and type ruby script.rb
- Use the shebang method and place #! /usr/local/bin/ruby -w at the top of the file, and make the file executable
What is the Ruby documentation system called?
RDoc