Skip to content

Instantly share code, notes, and snippets.

@perfectfoolish
Last active December 25, 2015 13:59
Show Gist options
  • Save perfectfoolish/6987974 to your computer and use it in GitHub Desktop.
Save perfectfoolish/6987974 to your computer and use it in GitHub Desktop.
def call_block
puts "Start of method"
yield
yield
yield
puts "End of method"
end
$greeting = "Hello"
@name = "Prudence"
def say_goodnight name
"Good night #{name.capitalize}"
end
a = [1, 'cat', 3.14]
b = %w{ant bee cat dog elk}
print b, "\n"
a[2] = nil
print a, "\n"
puts say_goodnight 'lifulei'
puts "#$greeting, #@name"
#while line = gets
# puts line.downcase
#end
line = "Perl"
#if line =~ /Perl|Python/
# puts "Scripting language mentioned: #{line}"
#end
newline = line.sub(/Perl/, 'Ruby')
puts newline
animals = %w{ ant bee cat dog elk }
def call_block
puts "Start of method"
yield
puts "End of method"
end
call_block {|str| puts animals }
animals.each {|name| print name, " " }
5.times { print "*"}
3.upto(6) {|i| print i}
('a'..'e').each {|char| print char}
puts " "
printf("Number: %5.2f, \nString: %s\n", 1.23, "hello")
line = gets
print line
print
class Song
@@plays = 0
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
@plays = 0
end
attr_reader :name, :artist, :duration
=begin
def name
@name
end
def artist
@artist
end
=end
attr_writer :name, :duration
=begin
def duration=(new_duration)
@duration = new_duration
end
=end
def duration_in_minutes
@duration/60
end
def duration_in_minutes=(new_duration)
@duration = (new_duration*60).to_i
end
def to_s
"Song: #@name -- #@artist (#@duration)"
end
def play
@plays += 1
@@plays += 1
"This song: #@plays play. Total #@@plays play."
end
def play
@plays += 1
@@plays += 1
"This song: #@plays plays. Total #@@plays plays."
end
end
class SongList
MAX_TIME = 5*60
def SongList.is_too_long(song)
return song.duration > MAX_TIME
end
end
class KaraokeSong < Song
def initialize(name, artist, duration, lyrics)
super(name, artist, duration)
@lyrics = lyrics
end
def to_s
# puts "Song: #@name -- #@artist (#@duration) [#@lyrics]"
super + " [#@lyrics]"
end
end
class SongList
MAX_TIME = 5*60
def SongList.is_too_long(song)
return song.duration > MAX_TIME
end
end
class MyLogger
private_class_method :new
@@logger = nil
def MyLogger.create
@@logger = new unless @@logger
@@logger
end
end
class Shape
def initialize(num_sides, perimeter)
@num_sides = num_sides
@perimeter = perimeter
end
def Shape.triangle(side_length)
Shape.new(3, side_length*3)
end
def Shape.square(side_length)
Shape.new(4, side_length*4)
end
end
song = Song.new("Bicyclops", "Fleck", 260)
puts song.inspect
puts song.to_s
new_song = KaraokeSong.new("My Way", "Sinatra", 255, "And now, the ...")
puts new_song.to_s
puts song.name
puts song.artist
puts song.duration
song.duration = 300
puts song.duration
song.name = "lifulei"
puts song.name
puts song.duration_in_minutes
song.duration_in_minutes = 6
puts song.duration
puts song.duration_in_minutes
s1 = Song.new("Song1", "Artist1", 100)
s2 = Song.new("Song2", "Artist2", 400)
puts s1.play
puts s2.play
puts s1.play
puts SongList.is_too_long(s1)
puts SongList.is_too_long(s2)
puts MyLogger.create.object_id
puts MyLogger.create.object_id
puts MyLogger.create.object_id
person = "Tim"
puts person.object_id
puts person.class
puts person
a = [1, 3, 5, 7, 9]
b = %w[2 4 6 d e f g]
puts a[-1]
puts a[-2]
a[2,2] = 'cat'
a[2,0] = 'dog'
a[1,1] = [9, 8, 7]
print a, "\n"
print b, "\n"
h = {'dog' => 'canine', 'cat' => 'feline', 'donkey' => 'asinine'}
h['cow'] = 'bovine'
h[12] = 'dodecine'
h['cat'] = 99
print h, "\n"
puts h.length
puts h['dog']
puts h[12] = 'dodecine'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment