METHODS, BLOCKS, & SORTING Practice Makes Perfect - Lecture 8/19


Hello Guys,

Can someone explain if there is a bug in the editor as I have been able to complete the following code without if/else statements after a horrendous amounts of tries?

def greeter(name)
return "Welcome: " + name

def by_three?(int)
return “true” if int % 3 == 0
return "false"

puts by_three?(15)

It wouldn’t work when I added if/else statements. I am just shocked that it has worked without it.



the “if int %3 == 0” is basically an if/else statement where if that isn’t true it goes to the next line and returns false. I see no bug


Thank you so much for explaining that.


Why are you shocked? The code works fine. It is just overly complicated.
I would write it like this:

def greeter(name)
  "Welcome: #{name}"
def by_three?(int)
  (int % 3 == 0).to_s

The point here is that (int % 3 == 0) results in either true or false,
so - since you (for whatever reason) want to have the string “true”,
just have to convert into a string. This can be done by applying to_s.
An alternative would be to use string interpolation,

def by_three?(int)
  "#{int % 3 == 0}"

which also applies to_s.

No ‘return’ statement needed.



Ha yes I wasn’t shocked at the fact that it worked. It was more that when i was complicating it further with if and else statements it just wasn’t happening. I guess Ruby prefers simplicity then excessive/complicated coding.

I see how you have done your code. Its probably the best way to code but I wouldn’t have known this as I only learnt this in further lessons now.

I was actually stuck on the following too. I guess the question didn’t make any sense to me tbh:

Topic: Converting Between Symbols and Strings:


We have an array of strings we’d like to later use as hash keys, but we’d rather they be symbols.

  1. Create a new variable, symbols, and store an empty array in it.
  2. Use .each to iterate over the strings array.
  3. For each s in strings, use .to_sym to convert s to a symbol and use .push to add that new symbol to symbols.
  4. Print the symbols array.

I have now spent an hour on this trying to figure out what they mean by point ‘3’. It is rather confusing as I am understanding that for every ‘s’ within a ‘string’ it should convert it to a symbol but when it finally changed the code to the following it changed each string to a symbol including the ones that did not have a ‘s’ in them. I don’t know whether my understanding of this point was wrong or the question itself is wrong. Your thoughts would be appreciated.

strings = [“HTML”, “CSS”, “JavaScript”, “Python”, “Ruby”]
symbols = []
strings.each do |s|

print symbols



I don’t see a requirement in the task (and a reason why it should so)
that strings contain the letter ‘s’. Having said this, using Array#each
is not the best choice for this task (Array#map would be better, and if
you find the time, you could as an exercise try to achieve the same
result using Array#map and observe how much simpler the solution
becomes), but as the task explicitly states that Array#each should be
used, you did it well.

BTW, the way you are printing the result does not let you see whether
you really successfully created an array of symbols (you did, but you
can not verify it), so I suggest that you don’t use

print symbols


p symbols


print symbols.inspect


One more note to my last remarkt:

Here I made a mistake. Since you have an Array, of course you can,
even with the print method, recognize that the content consists of
symbols. You CAN use p too, but it’s not necessary in this case.



Alright thanks for your response buddy. I will surely check out the new instructions in my own work space as here I was quite limited to doing on the instructions they have provided otherwise i cant proceed onto next task if my instructions differ from theirs.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.