map hash keys in Ruby [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I have a hash
{"one"=>1, "two"=>2, "three"=>3}
What is the best way to map the keys to get the array:
["one", "two", "three"]

{"one"=>1, "two"=>2, "three"=>3}.keys #=> ["one", "two", "three"]
The documentation is awesome for discovering answers to questions like these.
If the question is "how do I get X data from object of type Y?", reading through the docs for object type Y should be the first place you go.

{"one"=>1, "two"=>2, "three"=>3}.keys

Related

Is a dump method in ruby similar to perl's Data::Dumper->Dump(object) method? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
ruby newbie here.
The Data::Dumper->Dump(object) method in perl is awesome. It logs the entire structure of any object into stdout. Makes it really easy to debug. Basically looking for a method which can print the structure of any object irrespective of its type.
Is there a similar library/method in ruby?
Ruby has two such methods for debugging: p and pp.
The p(x) method is equivalent to:
puts x.inspect
The pp method is similar but pretty. That is it gives a more human-readable output.

How do you delete a key-value pair from a hash in ruby? I get an error when trying? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
myhash = {answer: "yes", something: hello, another: "yes"}
myhash.delete[another]
I want to delete the another key-value pair. But, ruby gives me an error saying wrong number of arguments (0 for 1). What's going on?
the method delete is a method, not an element on the hash , and another key is a symbol, so you should call on this form
myhash.delete(:another)

Ruby tool to reformat array literals [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
Is there a Ruby tool or editor plugin that will convert an array literal into a %w array literal?
ARRAY = [
"Foo",
"Bar",
"Baz"
]
ARRAY = %w(
Foo
Bar
Baz
)
I know I could write my own regex to do this, but I'm hoping for something off-the-shelf. I'm already using Rubocop as a linter, but it doesn't cover it. I can't seem to find a plugin for RubyMine, Sublime, or Atom for it either.
You can use rubocop -a to autocorrect offenses. One of the default cops is the word array cop, which should have you covered.

How do I get a random sample from an array? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is there a way to pull a random element from a list? Like say, picking a random name from a list of 10 names.
You can use Array#sample if the list is an array:
names.sample
Otherwise, if it's a string, you can convert it into an array first:
names.split.sample

Remove last two digits of number in Ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a number that comes from an external API but it has two extra zeros at the end. What would be a solution to remove the two zeros. I have tried the chomp method and that doesn't work. The number I receive from the API is a Bignum. I am using Ruby 2.
This is a sample input from the API is 1374577200000
This is a sample output I want is 13745772000
If the number is an integer, you can use integer division:
> 123456 / 100
=> 1234
You can't chomp a Fixnum or Bignum, so you'd have to convert it back and forth:
number = 123400
number.to_s[0..-3].to_i

Resources