#Message passing puts "the ruby language".length puts "Ruby".index("y") puts -6.abs puts 10.49.round puts 10.50.round puts 2.next puts 99.chr puts #Variables and Aliasing person1 = "Tommy" person2 = person1 #assigning person1 to person2 does not create an new objet. Both refer to the same object. puts person1 puts person2 puts #To void aliasing, and create new object with same content use dup. person3 = person1.dup person1[0] = "R" puts person1 puts person2 puts person3 #new object puts #Chaining assignment statements #assignment sets variable on left to experssion on right. a = b = 1 + 2 + 3 puts a puts b puts a = (b = 1 + 2) + 3 puts a puts b puts #Parallel assigment a = 1 b = 2 a,b = b,a puts a puts b puts x = 0 a, b, c = x, (x += 1), (x +=1) puts a # x = 0 puts b # x = 0 + 1 now x = 1 puts c # x = 1 + 1 now x = 2 puts x # x = 2 puts #Arrays #using literals : list of objects between brackets #arrays can hold objects of different types a = ["number",1,2,3.14] puts a puts a.length puts a[0] a[3] = nil puts a puts #Array object #Ruby allows array range specification myarray = [1,2,3,4,5,6] puts myarray[0] #1 puts puts myarray[1,3] #i to j,including j puts puts myarray[1...3] #i to j,excluding j puts puts myarray[1..3] #i to j,including j puts #Negative index allowed a = ["pi", 3.14, "prime",17] puts a.class puts a[-1] #count from end of array puts a[3] puts a[-2] puts a[2] puts a[1] puts a[0] puts b = Array.new puts b.class puts b.length b[0] = "a" b[1] = "new" puts b.length puts b puts #Associative Arrays (Hash) #undordered collection of elements #element is a pair of two objects: #value and key: #hashName = { "key" => "value",...} biblio = { "hello" => "No", "Salut" => "Yes", "Tell me the gossip fam" => "maybe" } puts biblio.length #To retreive the value of a key: #["key"] = value puts biblio["hello"] biblio["Tell me the gossip fam"] = "ok" puts biblio["Tell me the gossip fam"] puts #Add to the collection biblio["Someone is in love"] = "yas" puts biblio["Someone is in love"] puts biblio.length #Delete an element biblio.delete_if{|key, value| key == "hello"} puts biblio.length puts #Iterate over an associative array biblio.each_pair do |key,value| puts "#{key} : #{value}" end puts #Same output different way biblio.each { |key, value| puts key + " : " + value} puts #Iterate over collection and access and disply each key individually biblio.each_key {|key| puts key}
# Hello World Program in Ruby class Teste def soma(a,b) a+b end end inicial = Teste.new puts "Total:",inicial.soma(5,7)
# Hello World Program in Ruby puts "Hello World!"; # Hello World Program in Ruby students = Hash.new("students") students = ["id"=>"283560", "name"=>"shravan", "course"=>"Technology"] students.each do |key, value| print key, value end
# Hello World Program in Ruby puts "Hello World!"; $global_variable = 10000 class Student @@college_name = "Eastern Illinois University" def initialize(id, name, course) @stu_id = id @stu_name = name @stu_course = course end def display_student_details puts "College Name = #@@college_name" puts "student id = #@stu_id" puts "student name = #@stu_name" puts "Student course = #@stu_course" puts "Global Variable = #$global_variable" end end # creating object for Sutdent class object1 = Student.new("283560", "shravan", "Technology") object1.display_student_details object2 = Student.new("283560", "Divya", "Technology") object2.display_student_details
a=[0,1,2,3,4,5,6,7,8,9,10] puts "enter a number in the range 1-10" num=8 sum=0 b=[] a.each do |i| a.each do |j| sum=i+j if sum==num b << i b << j print b b.clear end end end
#!/usr/bin/env ruby require "json" $output = [] $index = 0; def buildJsonArray(json) if json['buckets'].each_with_index { |event, indexEvent| if event['SENT'] and event['SENT']['buckets'] and event['SENT']['buckets'].length > 0 if event['SENT']['buckets'].each_with_index { |sent, indexStatus| if sent['RULE'] and sent['RULE']['buckets'] and sent['RULE']['buckets'].length > 0 if sent['RULE']['buckets'].each_with_index { |rule, indexRule| if rule['CHANNEL'] and rule['CHANNEL']['buckets'] and rule['CHANNEL']['buckets'].length > 0 if rule['CHANNEL']['buckets'].each_with_index { |channel, indexChannel| if channel['ERROR'] and channel['ERROR']['buckets'] and channel['ERROR']['buckets'].length > 0 if channel['ERROR']['buckets'].each_with_index { |error, indexReason| buildRow(event, sent, rule, channel, error, error) } end else buildRow(event, sent, rule, nil, nil, channel) end } end else buildRow(event, sent, rule, nil, nil, rule) end } end else buildRow(event, sent, nil, nil, nil, sent) end } end else buildRow(event, nil, nil, nil, nil, event) end } end end def buildRow(event, sent, rule, channel, error, rowCount) $output[$output.length] = { 'eventType' => event ? event['key'] : '', 'sent' => sent ? sent['key'] == 1 : '', 'ruleId' => rule ? rule['key'] : '', 'channel' => channel ? channel['key'] : '', 'errorType' => error ? error['key'] : '', 'count' => rowCount['doc_count'], 'campaign' => '' } end def buildJsonArrayCampaign(json) if json['buckets'].each_with_index { |campaign, indexCampaign| if campaign['SENT'] and campaign['SENT']['buckets'] and campaign['SENT']['buckets'].length > 0 if campaign['SENT']['buckets'].each_with_index { |sent, indexStatus| if sent['CHANNEL'] and sent['CHANNEL']['buckets'] and sent['CHANNEL']['buckets'].length > 0 if sent['CHANNEL']['buckets'].each_with_index { |channel, indexChannel| if channel['ERROR'] and channel['ERROR']['buckets'] and channel['ERROR']['buckets'].length > 0 if channel['ERROR']['buckets'].each_with_index { |error, indexReason| buildRowCampaign(campaign, sent, channel, error, error) } end else buildRowCampaign(campaign, sent, nil, nil, channel) end } end else buildRowCampaign(campaign, nil, nil, nil, sent) end } end else buildRow(event, nil, nil, nil, nil, event) end } end end def buildRowCampaign(campaign, sent, channel, error, rowCount) $output[$output.length] = { 'campaign' => campaign ? campaign['key'] : '', 'sent' => sent ? sent['key'] == 1 : '', 'ruleId' => '', 'channel' => channel ? channel['key'] : '', 'errorType' => error ? error['key'] : '', 'count' => rowCount['doc_count'], 'eventType' => '' } end eventJ = '{ "took": 10, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 6, "max_score": 0, "hits": [] }, "aggregations": { "CAMPAIGN": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "capaignId", "doc_count": 2, "SENT": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 0, "key_as_string": "true", "doc_count": 2, "CHANNEL": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "APP", "doc_count": 2, "ERROR": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "SEND_TOKENNOTFOUND", "doc_count": 2 }, { "key": "ERROR_1", "doc_count": 8 } ] } } ] } } ] } } ] }, "EVENT": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 1, "doc_count": 6, "SENT": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": 1, "key_as_string": "true", "doc_count": 4, "RULE": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "5be1d3e12b0e2479436fa653", "doc_count": 4, "CHANNEL": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "APP", "doc_count": 4, "ERROR": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [] } } ] } } ] } }, { "key": 0, "key_as_string": "false", "doc_count": 2, "RULE": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "5be1d3e12b0e2479436fa653", "doc_count": 2, "CHANNEL": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "APP", "doc_count": 2, "ERROR": { "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0, "buckets": [ { "key": "SEND_TOKENNOTFOUND", "doc_count": 2 }, { "key": "ERROR_1", "doc_count": 8 } ] } } ] } } ] } } ] } } ] } } }' json = JSON.parse(eventJ) buildJsonArray(json['aggregations']['EVENT']) buildJsonArrayCampaign(json['aggregations']['CAMPAIGN']) puts($output) #puts JSON.pretty_generate(json)
def viginere(string) countArr = [] count=0 newString=[] for i in 1..string.length-1 do for x in i..string.length do newString.push(string[x]) end for y in 0..newString.length do if string[y] == newString[y] count = count + 1 end end countArr.push([i, count]) count=0 newString=[] end countArr = countArr.sort {|x,y| y[1] <=> x[1] } mods = [] for x in 0..20 do mods.push([x+2,0]) for i in 0..9 do if countArr[i][0]%(x+2) == 0 mods[x][1]=mods[x][1]+1 end end end mods = mods.sort {|x,y| y[1] <=> x[1] } return mods end def encrypt(string, key) result = [] i = 0 for x in 0..string.length-1 do pass_char = key[i] xor = string[x].ord ^ pass_char.ord result.push(xor) i+=1 if i == (key.size) i = 0 end end return result end password = ["J","O","N"] input = "had i the heavens embroidered cloths enwrought with golden and silver light the blue and the dim and the dark cloths of night and light and the half light i would spread the cloths under your feet but i being poor have only my dreams i have spread my dreams under your feet tread softly because you tread on my dreams" newText = encrypt(input, password) result = viginere(newText) for x in 0..result.length-1 do puts result[x][0].to_s + ": " + result[x][1].to_s + " matches" end
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more