[Solved] Length of input characters in Ruby

Why they are different ? It’s because in the second example there is a newline symbol also counted \n. Check it: print “Your age:” age=gets print age “33\n” print age.chomp.length # without newline #> 2 print age.length # with newline #> 3 chomp Returns a new String with the given record separator removed from the … Read more

[Solved] How to use string interpolation

If you intended the variable assignment to be Ruby code, then it is wrong. It should be person = “John”; building = “Big Tower” or person, building = “John”, “Big Tower” And for the question, yes, except that interpolation is a feature of Ruby, not Rails. Please be respectful to plain Ruby and its developers, … Read more

[Solved] How to do recurring deposit calculations accurately [closed]

If the first month calculation is correct and subsequent months are wrong, are you saying that you want a compound interest formula? (i.e. in month 2 you calculate interest on principle + previous months’ interest) toal = deposit_amount * (rate_of_interest*30/365)**month_number 1 solved How to do recurring deposit calculations accurately [closed]

[Solved] Parse of .txt on ruby

You can use CSV for parsing files with separators, e.g.: require ‘csv’ CSV.foreach(‘your_file.txt’, col_sep: “\t”, headers: true).map do |row| row.to_h end #=> [{“purchaser”=>”João”, “name”=>”St”, “item”=>”R$20”, “description”=>”off” …}, # {“purchaser”=>”Amy”, “name”=>”Rd”, “item”=>”awesome”, “description”=>”of”, ..}, …] It seems like this data is ready to process. A more common way is using comma-separated value for files like this, … Read more

[Solved] Accessing ruby elements

arr[-1] gives you the last element of arr, arr[-2] the second-to-last element and so on. arr[2, 3] gives you from the element at index 2, three elements of arr arr[2..3] gives you from the element at index 2 to the element at index 3 2 solved Accessing ruby elements

[Solved] The most annoying quirk of class methods in ruby ever

Like many object-oriented languages, Ruby has separation between methods in the class context and those in the instance context: class Example def self.a_class_method “I’m a class method!” end def an_instance_method “I’m an instance method!” end end When calling them using their native context it works: Example.a_class_method Example.new.an_instance_method When calling them in the wrong context you … Read more

[Solved] Counting changes in an array

Below one is solution I believe. Sure there is more room to refactor it, you can start from here. a = [“red”, “orange”, “green”, “red”, “yellow”, “blue”, “green”] a.reject {|e| ![‘red’, ‘green’].include? e } .each_cons(2) .select{|e| e == [‘red’, ‘green’]} .size A more artistic version. def neither_red_nor_green e ![‘red’, ‘green’].include? e end def red_followed_by_green ary … Read more

[Solved] What constitutes a stack level too deep error? Code Kata

Your zeros function is trying to do too much. It can’t calculate a factorial and at the same time count trailing zeroes in it. If you think about it, while you are calculating the factorial, number of trailing zeroes may (and will) change. You’re only interested in the final value. So first calculate that and … Read more

[Solved] Self Keyword in ruby [closed]

Call the method. If you get a NoMethodError: undefined method it actually isn’t defined. If it runs, then it is defined. It could be that it is defined in an external package the project uses, and not in the actual codebase. Also you can shorten it like this: def is_fdi? !!get_fed_state[/fdi/i] end solved Self Keyword … Read more

[Solved] Please help me access nested array

To access the array, you need to use array index. Instead of using @messages_test = options[‘Messages’][‘IBMessageID’].to_s you need to use, to access to first element the arrayoptions[‘Messages’] array, below code @messages_test = options[‘Messages’][0][‘IBMessageID’].to_s You can iterate the array, if you wish, by using options[‘Messages’] each do |item| puts item[“IBMessageID”] # for illustration end 1 solved … Read more