Ruby Flashcards

1
Q

Ruby Iteration

A
some\_list.**each** **do** |this\_item|
    # We're inside the block.
    # deal with this\_item.
**end**
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Everything has a value in Ruby.

A

x = 10
y = 11
z = if x < y
true
else
false
end
z # => true

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

In Ruby, a symbol is all about who it is, not what it is.

A

irb(main):001:0>:george.object_id==:george.object_id
=> true
irb(main):002:0>“george”.object_id==“george”.object_id
=> false

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

Ruby Class definition

A
  • *class** Person
    • *attr_accessor** :fname, :lname
   def **initialize**(fname, lname)
     @fname = fname
     @lname = lname
   end

def to_s
@lname + “, “ + @fname
end

def self.find_by_fname(fname)
found = nil
ObjectSpace.each_object(Person) { |o|
found = o if o.fname == fname
}
found
end
end

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