Use LEFT and RIGHT arrow keys to navigate between flashcards;
Use UP and DOWN arrow keys to flip the card;
H to show hint;
A reads text to speech;
21 Cards in this Set
- Front
- Back
How to print to screen?
|
"puts <string>"
"p <object>" (also prints object type) |
|
How to output to screen without newline?
|
print
|
|
How to evaluate variable inside double quotes?
|
use "test #{ variable}"
|
|
How to open and close a file?
|
aFile = File.new("filename", "mode")
# ... process the file aFile.close |
|
How to read the first 20 characters in a file?
|
sysread can be used to read the contents of a file
e.g. content = aFile.sysread(20) |
|
how to read the lines of a textfile?
|
arr = IO.readlines("fileltest.txt")
|
|
How to read lines with Ruby?
|
File.open('test.txt').each_line{ |s|
puts s } or File.foreach('test.txt') { |s| puts s } close file |
|
What is the output?
def callBlock yield yield end callBlock { puts "In the block" } |
In the block
In the block |
|
What does this produce?
a = %w( ant bee cat dog elk ) # create an array a.each { |animal| puts animal } # iterate over the contents |
ant
bee cat dog elk |
|
What is the effect of this?
a = %w{ ant bee cat dog elk } |
an array of string with values e.g.
a[0] >> "ant" a[3] >> "dog" |
|
How do you define a dictionary?
|
instSection = {
'cello' => 'string', 'clarinet' => 'woodwind', 'drum' => 'percussion', 'oboe' => 'woodwind', 'trumpet' => 'brass', 'violin' => 'string' } |
|
Give an example of simple iteration over array
|
{1,2,3].each do | value |
puts value end |
|
give different ways to create an array with values: vitamins, minerals and chocolates
|
# Make a list of clues for a game of charades
clues = ['vitamins', 'minerals', 'chocolates'] # Create the same array by splitting on whitespace clues = %w(vitamins minerals chocolates) # Create the same array in steps clues = Array.new clues << 'vitamins' clues << 'minerals' clues << 'chocolates' |
|
how can you pass arguments to a block?
|
using | arg | in the start of the block
|
|
how to match line with regex?
|
if line =~ /Perl|Python/
puts "Scripting language mentioned: #{line}" end |
|
how to control flow with if/else in ruby?
|
if customerName == "Fred" : print "Hello Fred!"
elsif customerName == "John" : print "Hello John!" elsif customername == "Robert" : print "Hello Bob!" end |
|
how to access substrings?
|
string = "this is an example"
==>"this is an example" value = string[2..6] ==>"is is" string[11..-1] = "elephant"; string ==>"this is an elephant" exit |
|
how to loop of i = 0 to 10
|
for i in (1..10).to_a
.... end |
|
Meta programming:
How to access meta information of a class? |
instance_variable_get '@event'
instance_variable_set '@region', 'UK' instance_variables 5.public_methods |
|
How to create instance variables with introspection?
|
class Person
def initiliaze(attributes) attributes.each do | attr, value | instance_variable_set("@#{attr}", val) end end end Person.new :name => 'Peter', :age => '31', :sex => :male |
|
why are instance variables marked with @ ?
|
with the mark @ with can identify variables as instance variables and do not need getters and setters
|