为什么我在Ruby中没有得到相同的输出

时间:2018-06-26 17:08:25

标签: ruby hashmap

在两种情况下,我没有得到一致的输出:

方案1:

public void getFitnessData() {


        Request request = new Request();
        request.setAccess_token("d80fa6bd6f78cc704104d61146c599bc94b82ca225349ee68762fc6c70d2dcf0");
        request.setEnd_date("2018-07-01T00:00:00");
        Flowable<Response> fitnessFlowable = new WebRequest()
                .getRemoteClient()
                .create(FitnessApi.class)
                .getFitnessData("5b238abb4d3590001d9b94a8",request.toMap());


         fitnessFlowable.subscribeOn(Schedulers.io())
                .takeUntil(response->response.getSummary().getNext()!=null)
                .doOnNext((response) -> {
                    if(response ==null || response.getFitness() == null || response.getFitness().isEmpty()) {


                        Log.e(TAG, " Error ");
                        return;
                    }

                    RxBus.getInstance().send(response.getFitness());

                    try(Realm r = Realm.getDefaultInstance()) {
                        r.executeTransaction((realm) -> {
                            realm.copyToRealmOrUpdate(response.getFitness());
                        });
                    }
                }).subscribe(item ->{


                 },
                 error ->{

                     RxBus.getInstance().error(error);


                 });
    }

方案2:

humen = {"hand" => 1, "eye" => 2, "head" => 3, "hair"=>4}
puts  "enter any body part name"
internal = gets.chomp.downcase.to_s

body = humen[internal]

puts body
#if input is "eye", it comes out 2

我在irb控制台中看不到任何东西。谁能解释为什么会这样吗?

1 个答案:

答案 0 :(得分:6)

键在第二种情况下是符号-

{:hand=>1, :eye=>2, :head=>3, :hair=>4}

internal字符串humen[internal]期望分配给internal的字符串出现在哈希humen中,情况并非如此。

:hand != 'hand'

您应通过以下方式将字符串转换为符号:

humen[internal.to_sym]

String#to_sym将字符串转换为符号。