Ruby to_s conversion to binary argument error - ruby

I was trying to solve a HackerRank problem which requires binary manipulation. The test cases are extremely huge numbers so I thought better to manipulate them as strings.
t=gets
def winner(pturn)
if a%2==0
puts "Richard\n"
else
puts "Louise\n"
end
end
while t != 0
turn=1
n=gets
(n2=n).to_s(2)
while n!=1
one="1"
zero="0"
if n2.count(1)>1
zero*=(n2.length - 2)
one.concat(zero)
n-=one.to_i(base=2)
else
n/=2
end
turn+=1
end
winner(turn)
t-=1
end
It caused an argument error (wrong number of argument) in line as seen below.
(n2=n).to_s(2)
I think I'm using to_s wrongly. I can't see the mistake and need someone to point it out.

I think you are trying to convert a number in Ruby to a string with it's binary representation. This is possible with (if the number is actually a number in Ruby, e.g. Fixnum) :
4.to_s 2
# => "100"
But in your case, what you are getting after calling gets is a string, and the to_s method of the String class simply returns itself, and doesn't take any arguments, hence the error.
You can fix it by calling gets.to_i instead of just gets, so the read string of contents will be converted to an integer in Ruby (you should be sure that you will only be reading numbers at there).
And I believe you are trying to assign the binary representation (as a string) of n into the variable n2, for that you should be doing
n2 = n.to_s(2)
If you just do :
(n2=n).to_s
Because of the parenthesis, first n2 will be assigned the value of n, then to_s will be called on n2, the string version of n2 will be returned, and nothing else happens. First thing you should be doing is the conversion, then the assignment.
And also you should pass in a string while calling String#count, i.e. you should call n2.count('2') instead of n2.count(2).

Argument error is coming because you are trying to pass parameter to to_s method because to_s method never accept any arguments.
So you should change (n2=n).to_s(2) to
(n2=n).to_s

Related

why pass block arguments to a function in ruby?

I'm unclear on why there is a need to pass block arguments when calling a function.
why not just pass in as function arguments and what happens to the block arguments, how are they passed and used?
m.call(somevalue) {|_k, v| v['abc'] = 'xyz'}
module m
def call ( arg1, *arg2, &arg3)
end
end
Ruby, like almost all mainstream programming languages, is a strict language, meaning that arguments are fully evaluated before being passed into the method.
Now, imagine you want to implement (a simplified version of) Integer#times. The implementation would look a little bit like this:
class Integer
def my_times(action_to_be_executed)
raise ArgumentError, "`self` must be non-negative but is `#{inspect}`" if negative?
return if zero?
action_to_be_executed
pred.my_times(action_to_be_executed)
end
end
3.my_times(puts "Hello")
# Hello
0.my_times(puts "Hello")
# Hello
-1.my_times(puts "Hello")
# Hello
# ArgumentError (`self` must be non-negative but is `-1`)
As you can see, 3.my_times(puts "Hello") printed Hello exactly once, instead of thrice, as it should do. Also, 0.my_times(puts "Hello") printed Hello exactly once, instead of not at all, as it should do, despite the fact that it returns in the second line of the method, and thus action_to_be_executed is never even evaluated. Even -1.my_times(puts "Hello") printed Hello exactly once, despite that fact that it raises an ArgumentError exception as the very first thing in the method and thus the entire rest of the method body is never evaluated.
Why is that? Because Ruby is strict! Again, strict means that arguments are fully evaluated before being passed. So, what this means is that before my_times even gets called, the puts "Hello" is evaluated (which prints Hello to the standard output stream), and the result of that evaluation (which is just nil because Kernel#puts always returns nil) is passed into the method.
So, what we need to do, is somehow delay the evaluation of the argument. One way we know how to delay evaluation, is by using a method: methods are only evaluated when they are called.
So, we take a page out of Java's playbook, and define a Single Abstract Method Protocol: the argument that is being passed to my_each must be an object which implements a method with a specific name. Let's call it call, because, well, we are going to call it.
This would look a little bit like this:
class Integer
def my_times(action_to_be_executed)
raise ArgumentError, "`self` must be non-negative but is `#{inspect}`" if negative?
return if zero?
action_to_be_executed.call
pred.my_times(action_to_be_executed)
end
end
def (hello = Object.new).call
puts "Hello"
end
3.my_times(hello)
# Hello
# Hello
# Hello
0.my_times(hello)
-1.my_times(hello)
# ArgumentError (`self` must be non-negative but is `-1`)
Nice! It works! The argument that is passed is of course still strictly evaluated before being passed (we can't change the fundamental nature of Ruby from within Ruby itself), but this evaluation only results in the object that is bound by the local variable hello. The code that we want to run is another layer of indirection away and will only be executed at the point where we actually call it.
It also has another advantage: Integer#times actually makes the index of the current iteration available to the action as an argument. This was impossible to implement with our first solution, but here we can do it, because we are using a method and methods can take arguments:
class Integer
def my_times(action_to_be_executed)
raise ArgumentError, "`self` must be non-negative but is `#{inspect}`" if negative?
__my_times_helper(action_to_be_executed)
end
protected
def __my_times_helper(action_to_be_executed, index = 0)
return if zero?
action_to_be_executed.call(index)
pred.__my_times_helper(action_to_be_executed, index + 1)
end
end
def (hello = Object.new).call(i)
puts "Hello from iteration #{i}"
end
3.my_times(hello)
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2
0.my_times(hello)
-1.my_times(hello)
# ArgumentError (`self` must be non-negative but is `-1`)
However, this is not actually very readable. If you didn't want to give a name to this action that we are trying to pass but instead simply literally write it down inside the argument list, it would look something like this:
3.my_times(Object.new.tap do |obj|
def obj.call(i)
puts "Hello from iteration #{i}"
end
end)
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2
or on one line:
3.my_times(Object.new.tap do |obj| def obj.call; puts "Hello from iteration #{i}" end end)
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2
# or:
3.my_times(Object.new.tap {|obj| def obj.call; puts "Hello from iteration #{i}" end })
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2
Now, I don't know about you, but I find that pretty ugly.
In Ruby 1.9, Ruby added Proc literals aka stabby lambda literals to the language. Lambda literals are a concise literal syntax for writing objects with a call method, specifically Proc objects with Proc#call.
Using lambda literals, and without any changes to our existing code, it looks something like this:
3.my_times(-> i { puts "Hello from iteration #{i}" })
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2
This does not look bad!
When Yukihiro "matz" Matsumoto designed Ruby almost thirty years ago in early 1993, he did a survey of the core libraries and standard libraries of languages like Smalltalk, Scheme, and Common Lisp to figure out how such methods that take a piece of code as an argument are actually used, and he found that the overwhelming majority of such methods take exactly one code argument and all they do with that argument is call it.
So, he decided to add special language support for a single argument that contains code and can only be called. This argument is both syntactically and semantically lightweight, in particular, it looks syntactically exactly like any other control structure, and it is semantically not an object.
This special language feature, you probably guessed it, are blocks.
Every method in Ruby has an optional block parameter. I can always pass a block to a method. It's up to the method to do anything with the block. Here, for example, the block is useless because Kernel#puts doesn't do anything with a block:
puts("Hello") { puts "from the block" }
# Hello
Because blocks are not objects, you cannot call methods on them. Also, because there can be only one block argument, there is no need to give it a name: if you refer to a block, it's always clear which block because there can be only one. But, if the block doesn't have methods and doesn't have a name, how can we call it?
That's what the yield keyword is for. It temporarily "yields" control flow to the block, or, in other words, it calls the block.
With blocks, our solution would look like this:
class Integer
def my_times(&action_to_be_executed)
raise ArgumentError, "`self` must be non-negative but is `#{inspect}`" if negative?
return enum_for(__callee__) unless block_given?
__my_times_helper(&action_to_be_executed)
end
protected
def __my_times_helper(&action_to_be_executed, index = 0)
return if zero?
yield index
pred.__my_times_helper(&action_to_be_executed, index + 1)
end
end
3.my_times do
puts "Hello from iteration #{i}"
end
# Hello from iteration 0
# Hello from iteration 1
# Hello from iteration 2
0.my_times do
puts "Hello from iteration #{i}"
end
-1.my_times do
puts "Hello from iteration #{i}"
end
# ArgumentError (`self` must be non-negative but is `-1`)
Okay, you might notice that I simplified a bit when I wrote above that the only thing you can do with a block is call it. There are two other things you can do with it:
You can check whether a block argument was passed using Kernel#block_given?. Since blocks are always optional, and blocks have no names, there must be a way to check whether a block was passed or not.
You can "roll up" a block (which is not an object and doesn't have a name) into a Proc object (which is an object) and bind it to a parameter (which gives it a name) using the & ampersand unary prefix sigil in the parameter list of the method. Now that we have an object, and a way to refer to it, we can store it in a variable, return it from a method, or (as we are doing here) pass it along as an argument to a different method, which otherwise wouldn't be possible.
There is also the opposite operation: with the & ampersand unary prefix operator, you can "unroll" a Proc object into a block in an argument list; this makes it so that the method behaves as if you had passed the code that is stored inside the Proc as a literal block argument to the method.
And there you have it! That's what blocks are for: a semantically and syntactically lightweight form of passing code to a method.
There are other possible approaches, of course. The approach that is closest to Ruby is probably Smalltalk. Smalltalk also has a concept called blocks (in fact, that is where Ruby got both the idea and the name from). Similarly to Ruby, Smalltalk blocks have a syntactically light-weight literal form, but they are objects, and you can pass more than one to a method. Thanks to Smalltalk's generally light-weight and simple syntax, especially the keyword method syntax which intersperses parts of the method name with the arguments, even passing multiple blocks to a method call is very concise and readable.
For example, Smalltalk actually does not have an if / then / else conditional expression, in fact, Smalltalk has no control structures at all. Everything is done with methods. So, the way that a conditional works, is that the two boolean classes TrueClass and FalseClass each have a method named ifTrue:ifFalse: which takes two block arguments, and the two implementations will simply either evaluate the first or the second block. For example, the implementation in TrueClass might look a little bit like this (note that Smalltalk has no syntax for classes or methods, instead classes and methods are created in the IDE by creating class objects and method objects via the GUI):
True>>ifTrue: trueBlock ifFalse: falseBlock
"Answer with the value of `trueBlock`."
↑trueBlock value
The corresponding implementation in FalseClass would then look like this:
FalseClass>>ifTrue: trueBlock ifFalse: falseBlock
"Answer with the value of `falseBlock`."
↑falseBlock value
And you would call it like this:
2 < 3 ifTrue: [ Transcript show: 'yes' ] ifFalse: [ Transcript show: 'no' ].
"yes"
4 < 3 ifTrue: [ Transcript show: 'yes' ] ifFalse: [ Transcript show: 'no' ].
"no"
In ECMAScript, you can simply use function definitions as expressions, and there is also lightweight syntax for functions.
In the various Lisps, code is just data, and data is code, so you can just pass the code as an argument as data, then inside the function, treat that data as code again.
Scala has call-by-name parameters which are only evaluated when you use their name, and they are evaluated every time you use their name. It would look something like this:
implicit class IntegerTimes(val i: Int) extends AnyVal {
#scala.annotation.tailrec
def times(actionToBeExecuted: => Unit): Unit = {
if (i < 0) throw new Error()
if (i == 0) () else { actionToBeExecuted; (i - 1).times(actionToBeExecuted) }
}
}
3.times { println("Hello") }
// Hello
// Hello
// Hello

bad value in range argument error - regarding range construction in ruby

I wrote the following snippet.
def add_me(num)
result = 0
(1..num).each { |i| result += i}
result
end
puts add_me(STDIN.gets)
I received an argument error list_sum.rb:6:in 'AddMe': bad value for range (ArgumentError) the line # corresponds to line # in my editor.
I also experimented with things like foo = (1..num).to_a. But still receive the same error. What is going on? Ruby version 2.3.3. What am I missing? I should be able to use variables in ranges, no?
gets returns a string. You need to do gets.to_i, in order to turn the input into a number for your numeric range. Right now you’re trying to make a range where the start is the number 1 and the end is some string, and that is raising an ArgumentError.
Also as an aside, ruby convention would tell you that your function should be named add_me. Ruby uses snake case, and anything that starts with a capital letter is typically assumed to be a class or constant (constant being all caps).

Getting "TypeError: no implicit conversion of Regexp into Integer" when trying to extract number from string

I'm using Rails 5. I'm having an issue extracting the numeric portion of a field and inserting it into my model attribute. I have
puts "age:#{age}"
self.age = age[/\d+/].present? ? age[/\d+/].to_i : nil
But I'm getting the output
age:41
TypeError: no implicit conversion of Regexp into Integer
from /Users/davea/Documents/workspace/myproject/app/models/my_object_time.rb:129:in `[]'
from /Users/davea/Documents/workspace/myproject/app/models/my_object_time.rb:129:in `set_age'
from /Users/davea/Documents/workspace/myproject/app/models/my_object_time.rb:51:in `block in <class:RaceTime>'
Is there a better way to write the above to extract the number (if ther eis one) but also avoid the error I'm seeing?
The error message is telling you that the [] you are calling expects an Integer as an argument, and thus it tries to convert the Regexp to an Integer (by calling to_int) but fails because Regexps don't respond to to_int.
Since String#[] does take a Regexp as an argument, it's obvious that the object referenced by age cannot be a String, it must be something else. Unfortunately, you don't tell us what object it is (you really should include such information in your question!) but we can make an educated guess: there are only three [] methods in Ruby I can think of, which would conceivably raise this exact error: Array#[], MatchData#[], and Integer#[]. So, my best guess at this point is that your age object is actually not a String, but rather a MatchData object, an Array, or an Integer.
But, we can do better: you included the result of to_s in your question. Array#to_s returns something like '[1, 2, 3]', but your question shows that age.to_s returns something like '41'. So, it can't be an Array.
Ergo, age is either a MatchData object or an Integer, but you are expecting it to be a String. You need to figure out where age is coming from, and why it isn't what you expect it to be, then fix that.
Your age variable is probably a number. I get the same error when I try to use the [/regex/] method with a number:
[5] pry(main)> 1[//]
TypeError: no implicit conversion of Regexp into Integer
You could say age = age.to_s first
I think you should better write that like:
str = "sometext 123 sometext"
self.age = /(?<age>\d+)/ =~ str.to_s ? age.to_i : nil

Beginner Ruby - about Objects

Beginner on Ruby here...below are two similar codes, I understand the procedures of this very basic code but I'd like to understand the theory behind it...
First
def stats(ppg)
if ppg > 20
puts "The PG is considered elite"
else
puts "The PG is not considered elite"
end
end
stats(28)
Second
def stats(ppg)
if ppg > 20
"The PG is considered elite"
else
"The PG is not considered elite"
end
end
puts stats(28)
So the first piece automatically writes out the strings whereas the second piece does not - is stats(ppg) from the first piece considered an object or a method with the variable as the argument?
def stats(ppg) in both cases is the same thing, i.e. the beginning of a method definition for a method named stats that takes one argument or parameter that will be named ppg inside the method body.
stats(28) in both cases is the same thing, i.e. a call to a method named stats with the literal Fixnum 28 passed as its argument, whereupon it will be assigned to the variable ppg inside the method body from the definition.
In Ruby, every expression involves two distinct phenomena: side effects, and return values. The expression a = 1 has a side effect of assigning the value of 1 to the variable a, and a return value of 1. Not every method in Ruby has a side effect, but every method does have a return value — either an explicit return value (by using return), or else the return value of the last expression evaluated in the method body.
puts is an unfortunate method in Ruby, because it is used in so many beginner examples, yet its behavior is confusing. It has a side effect of printing its argument to stdout, but its return value is nil (which often confuses beginners, who expect it to return the value of its argument).
The difference between your first method and second method is that the first method, because it uses puts internally, has the side effect of printing a string to stdout and a return value of nil, while the second method has no side effect, but a return value of the string itself.
Therefore, when you call your first method without puts in front, the side effect of printing the string occurs and you see the output. When you call your second method, there is no printing side effect, so in order to have the string printed to stdout, you have to call puts. The argument to puts is the return value of your stats method, i.e. the string you wanted to print.

Ruby block method help

I was trying to see if I could reconstruct the Array class' delete_if iterator as my own method in order to see if I understood methods and blocks correctly. Here is what I coded:
def delete_if(arr)
for x in 0...arr.length
if (yield arr[x])
arr[x]=arr[x+1,arr.length]
redo
end
end
end
arr = [0,1,2,3,4,5]
delete_if(arr) {|value| value % 2 == 0}
This resulted in an error saying that the % method could not be identified in the last line. I know that value is going to be an integer so I am not sure why it would say this error. Can someone please explain? Also, in Ruby in general, how can you be sure that someone passes the correct type into a method? What if the method is supposed to take a string but they pass in an integer -- how do you prevent that??
Thanks!
def delete_if arr
for x in 0...arr.length
return if x >= arr.length
if yield arr[x]
arr[x..-1] = arr[(x + 1)..-1]
redo
end
end
end
Things I fixed:
it's necessary to mutate the array, if all you do is assign to the parameter, your changes will be local to the method. And for that matter, you were assigning your calculated array object to an element of the original array, which was the immediate cause of the error message.
since the array may become shorter, we need to bail out at the (new) end
of course you could just use arr.delete_at x but I couldn't correct the slice assignment without keeping the code pattern

Resources