Friday, 1 July 2016

Fixing Bad Code the Wrong Way

Difficulty:
Easy

Tags:
method_missing, classes

Instructions:
Your coworker did a bad job defining a class. Fix it for him using #method_missing.

Problem and Solution:

# Solution to 'Fixing Bad Code the Wrong Way' on rubeque.com
# by jakthegr8
# http://www.rubeque.com/problems/fixing-bad-code-the-wrong-way
class Person
def initialize(name, age, incoming_race)
@name = name
@age = age
self.race = incoming_race
end
def nam
@name.split.map(&:capitalize).join(" ")
end
def agE
@age
end
def method_missing(m, *args)
if (m.to_s.downcase.match /name/)
"Kurt Vonnegut"
elsif (m.to_s.downcase.match /race/)
"Caucasian"
elsif (m.to_s.downcase.match /age/)
89
else
super
end
end
end
person = Person.new("kurt vonnegut", 89, "caucasian")
assert_equal person.name, "Kurt Vonnegut"
assert_equal person.race, "Caucasian"
assert_equal person.age, 89

Random Boolean

Difficulty:
Elementary
 
Tags:
rand, booleans

Instructions:
Return random booleans.

Problem and Solution:

# Solution to 'Random Boolean' on rubeque.com
# by jakthegr8
# http://www.rubeque.com/problems/random-boolean
def random_boolean
rand < rand
end
array = []
100.times do
if random_boolean
array << :dummy
end
end
assert_equal (1..100) === array.size, true
assert_equal array.size != 100, true

Thursday, 30 June 2016

The Truth

Difficulty:
Elementary

Tags:
booleans

Instructions:
Here's a hint: true equals true.

Problem and Solution:

# Solution to 'The Truth' on rubeque.com
# by jakthegr8
# http://www.rubeque.com/problems/the-truth
assert_equal true, !nil
view raw the_truth.rb hosted with ❤ by GitHub

Introduction

Ruby is a dynamic scripting language. Fairly easy to code and develop applications. Due to It's agility, flexibility and support availability many of the companies are using ruby. One of the popular ruby framework Rails, which is written in ruby completely.

Here, in this blog you can view and discuss many known problems and it's ruby solution. Hope this will help with people who are new to ruby also who solved many of the problems and want to see how it is solved in ruby. Please leave your comments incase if you need any help or want to discuss about any of the specific problems that I posted here