CONST = ' out there' class Inside_one CONST = proc {' in there'} def where_is_my_CONST ::CONST + ' inside one' end end class Inside_two CONST = ' inside two' def where_is_my_CONST CONST end end puts Inside_one.new.where_is_my_CONST puts Inside_two.new.where_is_my_CONST puts Object::CONST + Inside_two::CONST puts Inside_two::CONST + CONST puts Inside_one::CONST puts Inside_one::CONST.call + Inside_two::CONST
require 'optparse' require 'optparse/time' require 'ostruct' require 'pp' class OptparseExample CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary] CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" } # # Return a structure describing the options. # def self.parse(args) # The options specified on the command line will be collected in *options*. # We set default values here. options = OpenStruct.new options.library = [] options.books = [] options.download = [] options.xxxx = '' options.getnew = 0 options.inplace = false options.encoding = "utf8" options.transfer_type = :auto options.verbose = false opt_parser = OptionParser.new do |opts| opts.banner = "Usage: example.rb [options]" opts.separator "" opts.separator "Specific options:" # Mandatory argument. opts.on("-r", "--require LIBRARY", "Require the LIBRARY before executing your script") do |lib| options.library << lib end # Optional argument; multi-line description. opts.on("-i", "--inplace [EXTENSION]", "Edit ARGV files in place", " (make backup if EXTENSION supplied)") do |ext| options.inplace = true options.extension = ext || '' options.extension.sub!(/\A\.?(?=.)/, ".") # Ensure extension begins with dot. end # Cast 'days' argument to Integer. opts.on("--getnew N", Integer, "Update DB with books added in past N days") do |n| options.getnew = n end # Cast 'time' argument to a Time object. opts.on("-t", "--time [TIME]", Time, "Begin execution at given time") do |time| options.time = time end # List of arguments. opts.on("--books x,y,z", Array, "Specify books") do |list| options.books = list end # List of arguments. opts.on("--summarize x,y,z", Array, "Specify books") do |list| options.summarize = list end # List of arguments. opts.on("--download x,y,z", Array, "Download books") do |list| options.download = list end # Keyword completion. We are specifying a specific set of arguments (CODES # and CODE_ALIASES - notice the latter is a Hash), and the user may provide # the shortest unambiguous text. code_list = (CODE_ALIASES.keys + CODES).join(',') opts.on("--code CODE", CODES, CODE_ALIASES, "Select encoding", " (#{code_list})") do |encoding| options.encoding = encoding end # Optional argument with keyword completion. opts.on("--type [TYPE]", [:text, :binary, :auto], "Select transfer type (text, binary, auto)") do |t| options.transfer_type = t end # Boolean switch. opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| options.verbose = v end opts.separator "" opts.separator "Common options:" # No argument, shows at tail. This will print an options summary. # Try it and see! opts.on_tail("-h", "--help", "Show this message") do puts opts exit end # Another typical switch to print the version. opts.on_tail("--version", "Show version") do puts ::Version.join('.') exit end end opt_parser.parse!(args) options end # parse() end # class OptparseExample options = OptparseExample.parse(ARGV) options = OptparseExample.parse(['--download', 'DB1,DB2,DB3', '--books', 'DB1,DB2,DB3', '--getnew', '30']) pp options pp options.days, options.books options = OptparseExample.parse(['-h']) pp options pp ARGV
def remove_dups(head_node) unique_values = {} current_node = head_node prev_node = nil while current_node.next != nil if unique_values.key?(current_node.value) prev_node.next = current_node.next else unique_values[current_node.value] = true prev_node = current_node end current_node = current_node.next end current_node end
# the method should return an array containing all strings that are either shorter thatn 6 caracters or begin with "e", But not both def strange_words(array) arr = [] i = 0 while i < array.length char = array[i] if (char.length < 6 || char[0] == "e") && !(char.length < 6 && char[0] == "e") arr << char end i += 1 end return arr end p strange_words(["taco", "eggs", "excellent"])
# More variables and printing # Declaring varibles my_name = "Kavya Sree Kotagiri" my_age = "24" my_height = "5 feet 3 inches" my_eyes = "black" my_teeth = "white" my_hair = "black" # Printing them puts "Lets talk about #{my_name}." puts "She is #{my_height} feet." puts "Her age is #{my_age}." puts "She has #{my_eyes} eyes." # this line is tricky, try to get it same puts "If I add #{my_age}, #{my_height}. I get #{my_age + my_height}."
# Numbers and Maths puts "I will now count my chickens." # prints the statement mentioned puts "Hens #{25 + 30 / 6}" # prints the calculation done inside #{} puts "Rooster #{100 - 25 * 3 % 4}" # Same as above line puts "Now I will count eggs" # prints the statement mentioned puts 3 + 2 + 1 - 5 + 4 % 2 -1 / 4 + 6 # prints the result of calculation puts "Is it true that 3 + 2 < 5 - 7?" # prints the statement mentioned puts 3 + 2 < 5 - 7 # prints true or false puts "what is 3 + 2? #{3 + 2}" # prints the result of #{3 + 2} puts "what is 5 - 7? #{5 - 7}" # prints the result of #{3 + 2} puts "Oh, thats why its false." # prints the statement mentioned puts "How about some more" # prints the statement mentioned puts "Is it greater? #{5 > -2}" # prints the result as true or false puts "Is it greater or equal? #{5 >= -2}" # prints the result as true or false puts "Is it less or equal? #{5 <= -2}" # prints the result as true or false
def is_permutation(string_a, string_b) hash_a = {} hash_b = {} string_a.each_char do |char| if hash_a.key?(char) hash_a[char] += 1 else hash_a[char] = 1 end end string_b.each_char do |char| if hash_b.key?(char) hash_b[char] += 1 else hash_b[char] = 1 end end hash_a == hash_b end p is_permutation('abc', 'cba') #true p is_permutation('abc', 'cbb') #false p is_permutation('mortaal', 'arotmal') #true
def shortest_paths_to_source(graph, source) distance_to_source_for = {} prev_node_for = {} distance_to_source_for[source] = 0 pq = PriorityQueue.new graph.nodes.each do |node| if node != source distance_to_source_for[node] = +1.00/0.00 #infinity prev_node_for[node] = nil end pq.add_with_priority(node, distance_to_source_for[node]) end while !(pq.is_empty?) current_node = pq.pop #lowest priority node current_node.adjacent_nodes.each do |adjacent_node| distance = distance_to_source_for[current_node] + distance_between(current_node, adjacent_node) if distance < distance_to_source_for[adjacent_node] distance_to_source_for[adjacent_node] = distance prev_node_for[adjacent_node] = current_node pq.decrease_priority(adjacent_node, distance_to_source_for[adjacent_node]) end end end [distance_to_source_for, prev_node_for] end
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more