Skip to content

Instantly share code, notes, and snippets.

@eddywashere
Created March 13, 2014 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddywashere/9539021 to your computer and use it in GitHub Desktop.
Save eddywashere/9539021 to your computer and use it in GitHub Desktop.
old ruby notes

if

if tweets.empty?
  puts "no tweets found - better follow some ppl!"
end

# inline
puts "no tweets found - better follow some ppl!" if tweets.empty?

unless

unless tweets.empty?
  puts "timeline:"
  puts tweets
end

# inline (sort of, just missing the headline)
puts tweets unless tweets.empty?

if/unless with else

# bad
unless tweets.empty?
  puts "timeline:"
  puts tweets
else
  puts "no tweets found - better follow some ppl!"
end

# better
if tweets.empty?
  puts "no tweets found - better follow some ppl!"
else
  puts "timeline:"
  puts tweets
end

NIL is false-y

if attachment.file_path != nil
  attachment.post
end

if attachment.file_path
  attachment.post
end

# only nil is false-y
""  # treated as true
0   # treated as true
[]  # treated as true

# this will never be false
unless name.length
  warn "User name required"
end

** AND/OR**

# and
if user && user.signed_in?
  # do something for authenticated user
end

# or
if user.is_guest? || user.signed_in?
  # do something for guest or authenticated user
end

# result will equal 1
result = nil || 1
result = 1 || nil
result = 1 || 2

# default values
tweets = timeline.tweets || []

conditional assignment

i_was_set = 1
i_was_set ||= 2
puts i_was_set # -> 1

i_was_not_set ||= 2
puts i_was_not_set # -> 2

# bad
options[:country] = 'us' if options[:country].nil?
options[:privacy] = true if options[:privacy].nil?
options[:geotag]  = true if options[:geotag].nil?
# good
options[:country] ||= 'us'
options[:privacy] ||= true
options[:geotag]  ||= true

conditional return values

foo = true
text = if foo
  "hello world"
else
  "goodbye"
end
puts text
# -> hello world

# in methods
def list_url(user_name, list_name)
  if list_name
    "https://twitter.com/#{user_name}/#{list_name}"
  else
    "https://twitter.com/#{user_name}"
  end
end

optional arguments

def tweet(message, lat , long )
  # do something
end
tweet("Practicing Ruby-Fu!")

named arguments hash

def tweet(message, options = {})
  # do something
end
tweet("Practicing Ruby-Fu!")

# old hash syntax
tweet("Practicing Ruby-Fu!",
  :lat => 28.55,
  :long => -81.33,
  :reply_id => 227946
)

# new 1.9 hash syntax
tweet("Practicing Ruby-Fu!",
  lat: 28.55,
  long: -81.33,
  reply_id: 227946
)

exceptions

splat arguments

For sending in an array

def mention(status, *names)
  tweet("#{names.join(' ')} #{status}")
end
mention('Your courses rocked!', 'eallam', 'greggpollack', 'jasonvanlue')

Classes

class Name
  def initialize(first, last = nil)
    @first = first
    @last = last
  end
  def format
    [@last, @first].compact.join(', ')
  end
end

user_names = []
user_names << Name.new('Ashton', 'Kutcher')
user_names << Name.new('Madonna')

user_names.each { |n| puts n.format }

class Tweet
  attr_accessor :status
  attr_reader :created_at
  def initialize(status)
    @status = status
    @created_at = Time.new
  end
end

Re opening classes

class Tweet
  def to_s
    "#{@status}\n#{@created_at}"
  end
end

puts tweet.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment