Skip to content

Instantly share code, notes, and snippets.

@okram999
Last active September 6, 2016 19:15
Show Gist options
  • Save okram999/47494fac20a030381d57f72011350d4c to your computer and use it in GitHub Desktop.
Save okram999/47494fac20a030381d57f72011350d4c to your computer and use it in GitHub Desktop.
#Methods takes parameters as that of parameters in a script (ARGV)
def two_args(*argv)
a1, a2 = argv
puts "a1 = #{a1} & a2 = #{a2}"
end
puts two_args("one", "two")
============================================================
#File manipulation
#ARGV holds data in an array, hence the ARGV.first return the element in the 0th index
filename = ARGV.first
puts "we are going to delete the #{filename}"
puts "If you don't want that, hit CTRL-C (^C)."
puts "If you do want that, hit RETURN."
STDIN.gets.chomp
puts "opening the file..."
target = open(filename, 'w')
puts "truncating the file. Goodbye!"
target.truncate(0)
#target.close
puts "now i am going to ask you for lines"
print "line1: "
line1 = STDIN.gets.chomp
print "line2: "
line2 = STDIN.gets.chomp
puts "i am going to write these lines in the file #{filename}"
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
puts "Finally i am closing the file #{filename}"
target.close
==========================
##Requirement
#class Person
# def name
# @name
# end
# def name=(str)
# @name = str
# end
#end
##make it short
#class Person
# attr_reader :name
# attr_writer :name
#end
##even more shorter
class Person
attr_accessor :name
end
person = Person.new
puts person.name = 'nir'
puts person.name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment